@mathigon/core
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -25,4 +25,4 @@ 'use strict'; | ||
function applyDefaults(obj, defaults) { | ||
for (let key of Object.keys(defaults)) { | ||
if (!obj.hasOwnProperty(key)) | ||
for (const key of Object.keys(defaults)) { | ||
if (!Object.prototype.hasOwnProperty.call(obj, key)) | ||
obj[key] = defaults[key]; | ||
@@ -64,4 +64,4 @@ } | ||
function defer() { | ||
let resolve = (arg) => { }; | ||
let reject = (arg) => { }; | ||
let resolve = () => undefined; | ||
let reject = () => undefined; | ||
const promise = new Promise((_resolve, _reject) => { | ||
@@ -83,5 +83,5 @@ resolve = _resolve; | ||
function cache(fn) { | ||
let cached = new Map(); | ||
const cached = new Map(); | ||
return function (...args) { | ||
let argString = args.join('--'); | ||
const argString = args.join('--'); | ||
if (!cached.has(argString)) | ||
@@ -253,4 +253,4 @@ cached.set(argString, fn(...args)); | ||
function difference(a1, a2) { | ||
let notIn1 = a2.filter(a => !a1.includes(a)); | ||
let notIn2 = a1.filter(a => !a2.includes(a)); | ||
const notIn1 = a2.filter(a => !a1.includes(a)); | ||
const notIn2 = a1.filter(a => !a2.includes(a)); | ||
return [...notIn1, ...notIn2]; | ||
@@ -298,3 +298,3 @@ } | ||
function stringDistance(s1, s2, ignoreTrailing = false) { | ||
let arr = repeat2D(0, s1.length + 1, s2.length + 1); | ||
const arr = repeat2D(0, s1.length + 1, s2.length + 1); | ||
for (let i = 0; i <= s1.length; i++) | ||
@@ -306,4 +306,3 @@ arr[i][0] = i; | ||
for (let j = 1; j <= s2.length; j++) { | ||
arr[i][j] = Math.min(arr[i - 1][j - 1] + | ||
(s1.charAt(i - 1) === s2.charAt(j - 1) ? 0 : 1), arr[i - 1][j] + 1, arr[i][j - 1] + 1); | ||
arr[i][j] = Math.min(arr[i - 1][j - 1] + (s1.charAt(i - 1) === s2.charAt(j - 1) ? 0 : 1), arr[i - 1][j] + 1, arr[i][j - 1] + 1); | ||
} | ||
@@ -317,6 +316,6 @@ } | ||
function autoCorrect(word, dict) { | ||
let maxDistance = word.length / 2; | ||
let distances = dict.map(w => ({ w, d: stringDistance(word, w) })) | ||
const maxDistance = word.length / 2; | ||
const distances = dict.map(w => ({ w, d: stringDistance(word, w) })) | ||
.filter(({ d }) => d < maxDistance); | ||
let bestMatch = sortBy(distances, d => d.d)[0]; | ||
const bestMatch = sortBy(distances, d => d.d)[0]; | ||
return bestMatch ? bestMatch.w : undefined; | ||
@@ -333,3 +332,3 @@ } | ||
on(events, fn) { | ||
for (let e of words(events)) { | ||
for (const e of words(events)) { | ||
if (!this.events.has(e)) | ||
@@ -350,3 +349,3 @@ this.events.set(e, []); | ||
off(events, fn) { | ||
for (let e of words(events)) { | ||
for (const e of words(events)) { | ||
if (this.events.has(e)) { | ||
@@ -359,3 +358,3 @@ this.events.set(e, this.events.get(e).filter(x => x !== fn)); | ||
trigger(events, arg) { | ||
for (let e of words(events)) { | ||
for (const e of words(events)) { | ||
if (this.events.has(e)) { | ||
@@ -413,3 +412,4 @@ for (const callback of this.events.get(e)) { | ||
const min = Math.min(r, g, b); | ||
let h, s; | ||
let h; | ||
let s; | ||
const l = (max + min) / 2; | ||
@@ -416,0 +416,0 @@ if (max === min) { |
@@ -21,4 +21,4 @@ // ============================================================================= | ||
function applyDefaults(obj, defaults) { | ||
for (let key of Object.keys(defaults)) { | ||
if (!obj.hasOwnProperty(key)) | ||
for (const key of Object.keys(defaults)) { | ||
if (!Object.prototype.hasOwnProperty.call(obj, key)) | ||
obj[key] = defaults[key]; | ||
@@ -60,4 +60,4 @@ } | ||
function defer() { | ||
let resolve = (arg) => { }; | ||
let reject = (arg) => { }; | ||
let resolve = () => undefined; | ||
let reject = () => undefined; | ||
const promise = new Promise((_resolve, _reject) => { | ||
@@ -79,5 +79,5 @@ resolve = _resolve; | ||
function cache(fn) { | ||
let cached = new Map(); | ||
const cached = new Map(); | ||
return function (...args) { | ||
let argString = args.join('--'); | ||
const argString = args.join('--'); | ||
if (!cached.has(argString)) | ||
@@ -249,4 +249,4 @@ cached.set(argString, fn(...args)); | ||
function difference(a1, a2) { | ||
let notIn1 = a2.filter(a => !a1.includes(a)); | ||
let notIn2 = a1.filter(a => !a2.includes(a)); | ||
const notIn1 = a2.filter(a => !a1.includes(a)); | ||
const notIn2 = a1.filter(a => !a2.includes(a)); | ||
return [...notIn1, ...notIn2]; | ||
@@ -294,3 +294,3 @@ } | ||
function stringDistance(s1, s2, ignoreTrailing = false) { | ||
let arr = repeat2D(0, s1.length + 1, s2.length + 1); | ||
const arr = repeat2D(0, s1.length + 1, s2.length + 1); | ||
for (let i = 0; i <= s1.length; i++) | ||
@@ -302,4 +302,3 @@ arr[i][0] = i; | ||
for (let j = 1; j <= s2.length; j++) { | ||
arr[i][j] = Math.min(arr[i - 1][j - 1] + | ||
(s1.charAt(i - 1) === s2.charAt(j - 1) ? 0 : 1), arr[i - 1][j] + 1, arr[i][j - 1] + 1); | ||
arr[i][j] = Math.min(arr[i - 1][j - 1] + (s1.charAt(i - 1) === s2.charAt(j - 1) ? 0 : 1), arr[i - 1][j] + 1, arr[i][j - 1] + 1); | ||
} | ||
@@ -313,6 +312,6 @@ } | ||
function autoCorrect(word, dict) { | ||
let maxDistance = word.length / 2; | ||
let distances = dict.map(w => ({ w, d: stringDistance(word, w) })) | ||
const maxDistance = word.length / 2; | ||
const distances = dict.map(w => ({ w, d: stringDistance(word, w) })) | ||
.filter(({ d }) => d < maxDistance); | ||
let bestMatch = sortBy(distances, d => d.d)[0]; | ||
const bestMatch = sortBy(distances, d => d.d)[0]; | ||
return bestMatch ? bestMatch.w : undefined; | ||
@@ -329,3 +328,3 @@ } | ||
on(events, fn) { | ||
for (let e of words(events)) { | ||
for (const e of words(events)) { | ||
if (!this.events.has(e)) | ||
@@ -346,3 +345,3 @@ this.events.set(e, []); | ||
off(events, fn) { | ||
for (let e of words(events)) { | ||
for (const e of words(events)) { | ||
if (this.events.has(e)) { | ||
@@ -355,3 +354,3 @@ this.events.set(e, this.events.get(e).filter(x => x !== fn)); | ||
trigger(events, arg) { | ||
for (let e of words(events)) { | ||
for (const e of words(events)) { | ||
if (this.events.has(e)) { | ||
@@ -409,3 +408,4 @@ for (const callback of this.events.get(e)) { | ||
const min = Math.min(r, g, b); | ||
let h, s; | ||
let h; | ||
let s; | ||
const l = (max + min) / 2; | ||
@@ -412,0 +412,0 @@ if (max === min) { |
{ | ||
"name": "@mathigon/core", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "TypeScript utilities library containing function wrappers, string and array helper functions, event classes and color utilities.", | ||
@@ -25,14 +25,45 @@ "keywords": [ | ||
"test": "ts-node node_modules/tape/bin/tape test/**/*.ts", | ||
"prepublish": "npm test && npm run build", | ||
"build": "rollup --config" | ||
"prepublish": "npm test && npm run lint && npm run build", | ||
"build": "rollup --config", | ||
"lint": "eslint . --ext .ts" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-typescript": "^3.1.1", | ||
"@types/tape": "^4.2.33", | ||
"rollup": "^2.3.4", | ||
"tape": "^4.11.0", | ||
"ts-node": "^8.4.1", | ||
"tslib": "^1.10.0", | ||
"typescript": "^3.7.2" | ||
"@rollup/plugin-typescript": "5.0.2", | ||
"@types/tape": "4.13.0", | ||
"@typescript-eslint/eslint-plugin": "3.8.0", | ||
"@typescript-eslint/parser": "3.8.0", | ||
"eslint": "7.6.0", | ||
"eslint-config-google": "0.14.0", | ||
"eslint-plugin-import": "2.22.0", | ||
"rollup": "2.23.1", | ||
"tape": "5.0.1", | ||
"ts-node": "8.10.2", | ||
"tslib": "2.0.1", | ||
"typescript": "3.9.7" | ||
}, | ||
"renovate": { | ||
"extends": [ | ||
"config:base" | ||
], | ||
"schedule": [ | ||
"every weekend" | ||
], | ||
"packageRules": [ | ||
{ | ||
"updateTypes": [ | ||
"patch", | ||
"pin", | ||
"digest" | ||
], | ||
"automerge": true | ||
}, | ||
{ | ||
"packagePatterns": [ | ||
"eslint", | ||
"^@types" | ||
], | ||
"automerge": true | ||
} | ||
] | ||
} | ||
} |
@@ -38,3 +38,3 @@ // ============================================================================= | ||
export function tabulate2D<T>(fn: (i: number, j: number) => T, x: number, | ||
y: number): T[][] { | ||
y: number): T[][] { | ||
const result: T[][] = []; | ||
@@ -111,3 +111,3 @@ for (let i = 0; i < x; ++i) { | ||
return array.reduce((a, b) => | ||
a.concat(Array.isArray(b) ? flatten(b) : b), []); | ||
a.concat(Array.isArray(b) ? flatten(b) : b), []); | ||
} | ||
@@ -152,4 +152,4 @@ | ||
export function difference<T>(a1: T[], a2: T[]) { | ||
let notIn1 = a2.filter(a => !a1.includes(a)); | ||
let notIn2 = a1.filter(a => !a2.includes(a)); | ||
const notIn1 = a2.filter(a => !a1.includes(a)); | ||
const notIn2 = a1.filter(a => !a2.includes(a)); | ||
return [...notIn1, ...notIn2]; | ||
@@ -156,0 +156,0 @@ } |
@@ -8,3 +8,3 @@ // ============================================================================= | ||
/** A basic LRU cache implementation. */ | ||
export class Cache<T> { | ||
export class Cache<T> { | ||
private store = new Map<string, {val: T, i: number}>(); | ||
@@ -36,3 +36,3 @@ private list: string[] = []; | ||
this.list.push(a); | ||
this.store.set(a, {val: b, i: this.list.length}) | ||
this.store.set(a, {val: b, i: this.list.length}); | ||
@@ -39,0 +39,0 @@ // Remove the last item if necessary. |
@@ -60,3 +60,3 @@ // ============================================================================= | ||
let h, s; | ||
let h; let s; | ||
const l = (max + min) / 2; | ||
@@ -112,3 +112,3 @@ | ||
static fromHex(hex: string) { | ||
hex = hex.replace(shortHexRegex, function (m, r, g, b) { | ||
hex = hex.replace(shortHexRegex, function(m, r, g, b) { | ||
return r + r + g + g + b + b; | ||
@@ -115,0 +115,0 @@ }); |
@@ -19,3 +19,3 @@ // ============================================================================= | ||
on(events: string, fn: EventCallback) { | ||
for (let e of words(events)) { | ||
for (const e of words(events)) { | ||
if (!this.events.has(e)) this.events.set(e, []); | ||
@@ -37,3 +37,3 @@ this.events.get(e)!.push(fn); | ||
off(events: string, fn: EventCallback) { | ||
for (let e of words(events)) { | ||
for (const e of words(events)) { | ||
if (this.events.has(e)) { | ||
@@ -47,3 +47,3 @@ this.events.set(e, this.events.get(e)!.filter(x => x !== fn)); | ||
trigger(events: string, arg?: any) { | ||
for (let e of words(events)) { | ||
for (const e of words(events)) { | ||
if (this.events.has(e)) { | ||
@@ -50,0 +50,0 @@ for (const callback of this.events.get(e)!) { |
@@ -41,3 +41,3 @@ // ============================================================================= | ||
export function stringDistance(s1: string, s2: string, ignoreTrailing = false) { | ||
let arr = repeat2D(0, s1.length + 1, s2.length + 1); | ||
const arr = repeat2D(0, s1.length + 1, s2.length + 1); | ||
for (let i = 0; i <= s1.length; i++) arr[i][0] = i; | ||
@@ -48,4 +48,4 @@ for (let i = 0; i <= s2.length; i++) arr[0][i] = i; | ||
for (let j = 1; j <= s2.length; j++) { | ||
arr[i][j] = Math.min(arr[i - 1][j - 1] + | ||
(s1.charAt(i - 1) === s2.charAt(j - 1) ? 0 : 1), | ||
arr[i][j] = Math.min( | ||
arr[i - 1][j - 1] + (s1.charAt(i - 1) === s2.charAt(j - 1) ? 0 : 1), | ||
arr[i - 1][j] + 1, arr[i][j - 1] + 1); | ||
@@ -62,7 +62,7 @@ } | ||
export function autoCorrect(word: string, dict: string[]) { | ||
let maxDistance = word.length / 2; | ||
let distances = dict.map(w => ({w, d: stringDistance(word, w)})) | ||
const maxDistance = word.length / 2; | ||
const distances = dict.map(w => ({w, d: stringDistance(word, w)})) | ||
.filter(({d}) => d < maxDistance); | ||
let bestMatch = sortBy(distances, d => d.d)[0]; | ||
const bestMatch = sortBy(distances, d => d.d)[0]; | ||
return bestMatch ? bestMatch.w : undefined; | ||
} |
@@ -32,4 +32,4 @@ // ============================================================================= | ||
export function applyDefaults(obj: any, defaults: any) { | ||
for (let key of Object.keys(defaults)) { | ||
if (!obj.hasOwnProperty(key)) obj[key] = defaults[key]; | ||
for (const key of Object.keys(defaults)) { | ||
if (!Object.prototype.hasOwnProperty.call(obj, key)) obj[key] = defaults[key]; | ||
} | ||
@@ -76,4 +76,4 @@ return obj; | ||
export function defer<T = void>() { | ||
let resolve = (arg?: T) => {}; | ||
let reject = (arg?: any) => {}; | ||
let resolve: ((arg?: T) => void) = () => undefined; | ||
let reject: ((arg?: any) => void) = () => undefined; | ||
@@ -100,5 +100,5 @@ const promise = new Promise<T>((_resolve, _reject) => { | ||
export function cache<T>(fn: (...args: any[]) => T) { | ||
let cached = new Map<string, T>(); | ||
return function (...args: any[]) { | ||
let argString = args.join('--'); | ||
const cached = new Map<string, T>(); | ||
return function(...args: any[]) { | ||
const argString = args.join('--'); | ||
if (!cached.has(argString)) cached.set(argString, fn(...args)); | ||
@@ -105,0 +105,0 @@ return cached.get(argString)!; |
@@ -12,31 +12,31 @@ // ============================================================================= | ||
tape('repeat events', (t) => { | ||
const target = new EventTarget(); | ||
const target = new EventTarget(); | ||
let count = 0; | ||
target.on('increment', e => count++); | ||
let count = 0; | ||
target.on('increment', () => count++); | ||
target.trigger('increment'); | ||
target.trigger('increment'); | ||
target.trigger('increment'); | ||
target.trigger('increment'); | ||
target.trigger('increment'); | ||
target.trigger('increment'); | ||
setTimeout(() => { | ||
t.equal(count, 3); | ||
t.end(); | ||
}, 100); | ||
setTimeout(() => { | ||
t.equal(count, 3); | ||
t.end(); | ||
}, 100); | ||
}); | ||
tape('one-time events', (t) => { | ||
const target = new EventTarget(); | ||
const target = new EventTarget(); | ||
let count = 0; | ||
target.one('increment', e => count++); | ||
let count = 0; | ||
target.one('increment', () => count++); | ||
target.trigger('increment'); | ||
target.trigger('increment'); | ||
target.trigger('increment'); | ||
target.trigger('increment'); | ||
target.trigger('increment'); | ||
target.trigger('increment'); | ||
setTimeout(() => { | ||
t.equal(count, 1); | ||
t.end(); | ||
}, 100); | ||
setTimeout(() => { | ||
t.equal(count, 1); | ||
t.end(); | ||
}, 100); | ||
}); |
@@ -7,3 +7,2 @@ { | ||
], | ||
"outDir": "ts_build", | ||
"moduleResolution": "node", | ||
@@ -10,0 +9,0 @@ "strict": true, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62354
20
1716
12