-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add first tests and modify rollup config
- Loading branch information
Showing
8 changed files
with
2,451 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules/ | ||
dist/ | ||
coverage/ | ||
tmp/ | ||
yarn-error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Oops, something went wrong.