Skip to content

Commit

Permalink
Add first tests and modify rollup config
Browse files Browse the repository at this point in the history
  • Loading branch information
szimek committed Mar 15, 2018
1 parent 7221791 commit 6e4e68b
Show file tree
Hide file tree
Showing 8 changed files with 2,451 additions and 115 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
dist/
coverage/
tmp/
yarn-error.log
17 changes: 2 additions & 15 deletions docs/js/signature_pad.umd.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
/*!
* Signature Pad v3.0.0
* https://github.com/szimek/signature_pad
*
* Copyright 2018 Szymon Nowak
* Released under the MIT license
*
* The main idea and some parts of the code (e.g. drawing variable width Bézier curve) are taken from:
* http://corner.squareup.com/2012/07/smoother-signatures.html
*
* Implementation of interpolation using cubic Bézier curves is taken from:
* http://benknowscode.wordpress.com/2012/09/14/path-interpolation-using-cubic-bezier-and-control-point-estimation-in-javascript
*
* Algorithm for approximated length of a Bézier curve is taken from:
* http://www.lemoda.net/maths/bezier-length/index.html
*
* Signature Pad v3.0.0 | https://github.com/szimek/signature_pad
* (c) 2018 Szymon Nowak | Released under the MIT license
*/

(function (global, factory) {
Expand Down
33 changes: 26 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
"url": "https://github.com/szimek"
},
"license": "MIT",
"main": "dist/signature_pad.umd.js",
"module": "dist/signature_pad.es.js",
"types": "dist/signature_pad.d.ts",
"source": "src/signature_pad.ts",
"dev:main": "dist/signature_pad.development.js",
"main": "dist/signature_pad.js",
"module": "dist/signature_pad.m.js",
"umd:main": "dist/signature_pad.umd.js",
"types": "dist/types/signature_pad.d.ts",
"scripts": {
"build": "del dist && rollup --config && cp dist/signature_pad.umd.js docs/js/",
"prepublishOnly": "yarn run build"
"prepublishOnly": "yarn run build",
"test": "jest --coverage"
},
"repository": {
"type": "git",
Expand All @@ -28,10 +32,25 @@
"devDependencies": {
"del": "^3.0.0",
"del-cli": "^1.1.0",
"rollup": "^0.55.0",
"rollup": "^0.56.5",
"rollup-plugin-tslint": "^0.1.34",
"rollup-plugin-typescript2": "^0.11.1",
"rollup-plugin-typescript2": "^0.12.0",
"rollup-plugin-uglify": "^3.0.0",
"typescript": "^2.6.1"
"typescript": "^2.7.2",
"@types/jest": "22.2.0",
"jest": "22.4.2",
"ts-jest": "22.4.1"
},
"jest": {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testMatch": [
"<rootDir>/tests/**/*.ts"
],
"moduleFileExtensions": [
"ts",
"js"
]
}
}
144 changes: 85 additions & 59 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,107 @@

const typescript = require('rollup-plugin-typescript2');
const tslint = require('rollup-plugin-tslint');
const uglify = require('rollup-plugin-uglify');

const pkg = require('./package.json');

const plugins = (options = {}) => [
const plugins = (tsConfig = {}) => [
tslint(),
typescript({
tsconfigDefaults: {},
tsconfig: 'tsconfig.json',
tsconfigOverride: tsConfig,
typescript: require('typescript'),
cacheRoot: './tmp/.rts2_cache',
useTsconfigDeclarationDir: true,
typescript: require('typescript'),
tsconfigOverride: {
compilerOptions: options,
},
include: [ "src/**/*.ts" ],
}),
];

const longBanner = '/*!\n' +
` * Signature Pad v${pkg.version}\n` +
` * ${pkg.homepage}\n` +
' *\n' +
` * Copyright ${new Date().getFullYear()} ${pkg.author.name}\n` +
' * Released under the MIT license\n' +
' *\n' +
' * The main idea and some parts of the code (e.g. drawing variable width Bézier curve) are taken from:\n' +
' * http://corner.squareup.com/2012/07/smoother-signatures.html\n' +
' *\n' +
' * Implementation of interpolation using cubic Bézier curves is taken from:\n' +
' * http://benknowscode.wordpress.com/2012/09/14/path-interpolation-using-cubic-bezier-and-control-point-estimation-in-javascript\n' +
' *\n' +
' * Algorithm for approximated length of a Bézier curve is taken from:\n' +
' * http://www.lemoda.net/maths/bezier-length/index.html\n' +
' *\n' +
' */\n';

const shortBanner = '/*!\n' +
const banner = '/*!\n' +
` * Signature Pad v${pkg.version} | ${pkg.homepage}\n` +
` * (c) ${new Date().getFullYear()} ${pkg.author.name} | Released under the MIT license\n` +
' */\n';

export default [{
input: 'src/signature_pad.ts',
plugins: plugins({
target: 'ES3',
}),
output: {
file: 'dist/signature_pad.umd.js',
format: 'umd',
name: 'SignaturePad',
banner: longBanner,
export default [
// CJS unminified
{
input: 'src/signature_pad.ts',
plugins: plugins({
compilerOptions: {
target: 'ES3',
},
}),
output: {
file: 'dist/signature_pad.js',
format: 'cjs',
banner,
},
},
// CJS minified
{
input: 'src/signature_pad.ts',
plugins: [
...plugins({
compilerOptions: {
target: 'ES3',
},
}),
uglify()
],
output: {
file: 'dist/signature_pad.min.js',
format: 'cjs',
banner,
},
},
}, {
input: 'src/signature_pad.ts',
plugins: [
...plugins({
target: 'ES3',
// UMD unminified
{
input: 'src/signature_pad.ts',
plugins: plugins({
compilerOptions: {
target: 'ES3',
},
}),
uglify()
],
output: {
file: 'dist/signature_pad.umd.min.js',
format: 'umd',
name: 'SignaturePad',
banner: shortBanner,
output: {
file: 'dist/signature_pad.umd.js',
format: 'umd',
name: 'SignaturePad',
banner,
},
},
}, {
input: 'src/signature_pad.ts',
plugins: plugins({
target: 'ES5',
declaration: true,
}),
output: {
file: 'dist/signature_pad.es.js',
format: 'es',
banner: longBanner,
// UMD minified
{
input: 'src/signature_pad.ts',
plugins: [
...plugins({
compilerOptions: {
target: 'ES3',
},
}),
uglify()
],
output: {
file: 'dist/signature_pad.umd.min.js',
format: 'umd',
name: 'SignaturePad',
banner,
},
},
}];
// ES module minified
{
input: 'src/signature_pad.ts',
plugins: [
...plugins({
compilerOptions: {
target: 'ES5',
declaration: true,
},
}),
uglify()
],
output: {
file: 'dist/signature_pad.m.js',
format: 'es',
banner,
},
}
];
11 changes: 11 additions & 0 deletions src/signature_pad.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
/**
* The main idea and some parts of the code (e.g. drawing variable width Bézier curve) are taken from:
* http://corner.squareup.com/2012/07/smoother-signatures.html
*
* Implementation of interpolation using cubic Bézier curves is taken from:
* http://www.benknowscode.com/2012/09/path-interpolation-using-cubic-bezier_9742.html
*
* Algorithm for approximated length of a Bézier curve is taken from:
* http://www.lemoda.net/maths/bezier-length/index.html
*/

import { Bezier } from "./bezier";
import { IBasicPoint, Point } from "./point";
import { throttle } from "./throttle";
Expand Down
44 changes: 44 additions & 0 deletions tests/bezier.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* global global */

import { Bezier } from "../src/bezier";
import { Point } from "../src/point";

describe(".fromPoints", () => {
it("returns a new Bézier curve", () => {
const now = Date.now();

freezeTimeAt(now, () => {
const p1 = new Point(100, 25);
const p2 = new Point(10, 90);
const p3 = new Point(110, 100);
const p4 = new Point(132, 192);
const curve = Bezier.fromPoints([p1, p2, p3, p4], { start: 0.5, end: 2 });

expect(curve.startPoint).toEqual(p2);
expect(curve.control1).toEqual(new Point(78.57685352817168, 73.72818901535666));
expect(curve.control2).toEqual(new Point(12.375668721124931, 107.81751540843696));
expect(curve.endPoint).toBe(p3);
expect(curve.startWidth).toBe(0.5);
expect(curve.endWidth).toBe(2);
});
});
});

describe("#length", () => {
it("returns approximated length", () => {
const p1 = new Point(100, 25);
const p2 = new Point(10, 90);
const p3 = new Point(110, 100);
const p4 = new Point(132, 192);
const curve = new Bezier(p1, p2, p3, p4, 1, 1);

expect(curve.length()).toBe(196.92750351842562); // close enough ¯\_(ツ)_/¯
});
});

function freezeTimeAt(time: number, callback: (...args: any[]) => any) {
const now = Date.now;
Date.now = jest.fn().mockReturnValue(time);
callback();
Date.now = now;
}
63 changes: 63 additions & 0 deletions tests/point.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Point } from "../src/point";

describe("#distanceTo", () => {
it("returns distance to other point", () => {
const now = Date.now();
const a = new Point(0, 0, now);
const b = new Point(4, 3, now);

expect(a.distanceTo(b)).toBe(5);
});
});

describe("#equals", () => {
it("returns true if points have the same attributes", () => {
const now = Date.now();
const a = new Point(1, 1, now);
const b = new Point(1, 1, now);

expect(a.equals(b)).toBe(true);
});

it("returns false if points have the different 'x' attributes", () => {
const now = Date.now();
const a = new Point(1, 1, now);
const b = new Point(2, 1, now);

expect(a.equals(b)).toBe(false);
});

it("returns false if points have the different 'y' attributes", () => {
const now = Date.now();
const a = new Point(1, 1, now);
const b = new Point(1, 2, now);

expect(a.equals(b)).toBe(false);
});

it("returns false if points have the different 'time' attributes", () => {
const now = Date.now();
const a = new Point(1, 1, now);
const b = new Point(1, 1, now + 1);

expect(a.equals(b)).toBe(false);
});
});

describe("#velocityFrom", () => {
it("returns 0 if times are equal", () => {
const now = Date.now();
const a = new Point(1, 1, now);
const b = new Point(1, 1, now);

expect(a.velocityFrom(b)).toBe(0);
});

it("returns velocity if times are different", () => {
const now = Date.now();
const a = new Point(0, 0, now);
const b = new Point(4, 3, now + 10);

expect(a.velocityFrom(b)).toBe(-0.5);
});
});
Loading

0 comments on commit 6e4e68b

Please sign in to comment.