@mathigon/core
Advanced tools
Comparing version 0.6.6 to 0.6.7
@@ -24,3 +24,3 @@ 'use strict'; | ||
/** Applies default keys to an object. */ | ||
function applyDefaults(obj, defaults) { | ||
function applyDefaults(obj = {}, defaults) { | ||
for (const key of Object.keys(defaults)) { | ||
@@ -74,2 +74,8 @@ if (!Object.prototype.hasOwnProperty.call(obj, key)) | ||
} | ||
class CacheError extends Error { | ||
constructor(data) { | ||
super('[Cache Error]'); | ||
this.data = data; | ||
} | ||
} | ||
/** | ||
@@ -80,3 +86,3 @@ * Function wrapper that modifies a function to cache its return values. This | ||
* which are always called with different arguments. Note that argument | ||
* comparison doesn't not work with Objects or nested arrays. | ||
* comparison does not work with Objects or nested arrays. | ||
*/ | ||
@@ -87,5 +93,14 @@ function cache(fn) { | ||
const argString = args.join('--'); | ||
if (!cached.has(argString)) | ||
cached.set(argString, fn(...args)); | ||
return cached.get(argString); | ||
if (!cached.has(argString)) { | ||
try { | ||
cached.set(argString, fn(...args)); | ||
} | ||
catch (e) { | ||
cached.set(argString, new CacheError(e)); | ||
} | ||
} | ||
const value = cached.get(argString); | ||
if (value instanceof CacheError) | ||
throw value.data; | ||
return value; | ||
}; | ||
@@ -413,2 +428,6 @@ } | ||
} | ||
/** Get the brightness of this colour. */ | ||
get brightness() { | ||
return (this.r * 299 + this.g * 587 + this.g * 114) / 1000; | ||
} | ||
/** Converts this colour to an HSL array. */ | ||
@@ -600,2 +619,24 @@ get hsl() { | ||
} | ||
function* pairs(a, b) { | ||
for (const i of a) { | ||
for (const j of b) { | ||
yield [i, j]; | ||
} | ||
} | ||
} | ||
/** Find the item in an iterable for which value() returns the smallest value. */ | ||
function findMin(items, value, max = Infinity, min) { | ||
let best = undefined; | ||
let v = max; | ||
for (const item of items) { | ||
const v1 = value(item); | ||
if (v1 < v) { | ||
best = item; | ||
v = v1; | ||
if (min !== undefined && v < min) | ||
return best; | ||
} | ||
} | ||
return best; | ||
} | ||
class Itarray { | ||
@@ -704,2 +745,3 @@ constructor(...values) { | ||
exports.every = every; | ||
exports.findMin = findMin; | ||
exports.first = first; | ||
@@ -715,2 +757,3 @@ exports.flatMap = flatMap; | ||
exports.loop = loop; | ||
exports.pairs = pairs; | ||
exports.repeat = repeat; | ||
@@ -717,0 +760,0 @@ exports.repeat2D = repeat2D; |
@@ -20,3 +20,3 @@ // ============================================================================= | ||
/** Applies default keys to an object. */ | ||
function applyDefaults(obj, defaults) { | ||
function applyDefaults(obj = {}, defaults) { | ||
for (const key of Object.keys(defaults)) { | ||
@@ -70,2 +70,8 @@ if (!Object.prototype.hasOwnProperty.call(obj, key)) | ||
} | ||
class CacheError extends Error { | ||
constructor(data) { | ||
super('[Cache Error]'); | ||
this.data = data; | ||
} | ||
} | ||
/** | ||
@@ -76,3 +82,3 @@ * Function wrapper that modifies a function to cache its return values. This | ||
* which are always called with different arguments. Note that argument | ||
* comparison doesn't not work with Objects or nested arrays. | ||
* comparison does not work with Objects or nested arrays. | ||
*/ | ||
@@ -83,5 +89,14 @@ function cache(fn) { | ||
const argString = args.join('--'); | ||
if (!cached.has(argString)) | ||
cached.set(argString, fn(...args)); | ||
return cached.get(argString); | ||
if (!cached.has(argString)) { | ||
try { | ||
cached.set(argString, fn(...args)); | ||
} | ||
catch (e) { | ||
cached.set(argString, new CacheError(e)); | ||
} | ||
} | ||
const value = cached.get(argString); | ||
if (value instanceof CacheError) | ||
throw value.data; | ||
return value; | ||
}; | ||
@@ -409,2 +424,6 @@ } | ||
} | ||
/** Get the brightness of this colour. */ | ||
get brightness() { | ||
return (this.r * 299 + this.g * 587 + this.g * 114) / 1000; | ||
} | ||
/** Converts this colour to an HSL array. */ | ||
@@ -596,2 +615,24 @@ get hsl() { | ||
} | ||
function* pairs(a, b) { | ||
for (const i of a) { | ||
for (const j of b) { | ||
yield [i, j]; | ||
} | ||
} | ||
} | ||
/** Find the item in an iterable for which value() returns the smallest value. */ | ||
function findMin(items, value, max = Infinity, min) { | ||
let best = undefined; | ||
let v = max; | ||
for (const item of items) { | ||
const v1 = value(item); | ||
if (v1 < v) { | ||
best = item; | ||
v = v1; | ||
if (min !== undefined && v < min) | ||
return best; | ||
} | ||
} | ||
return best; | ||
} | ||
class Itarray { | ||
@@ -686,2 +727,2 @@ constructor(...values) { | ||
export { Cache, Color, EventTarget, Itarray, applyDefaults, autoCorrect, cache, chunk, cumulative, deepExtend, defer, delay, difference, every, first, flatMap, flatten, intersect, isOneOf, isPalindrome, join, last, list, loop, repeat, repeat2D, rotate, run, safeToJSON, some, sortBy, stringDistance, tabulate, tabulate2D, throttle, toCamelCase, toLinkedList, toTitleCase, total, uid, unique, wait, words }; | ||
export { Cache, Color, EventTarget, Itarray, applyDefaults, autoCorrect, cache, chunk, cumulative, deepExtend, defer, delay, difference, every, findMin, first, flatMap, flatten, intersect, isOneOf, isPalindrome, join, last, list, loop, pairs, repeat, repeat2D, rotate, run, safeToJSON, some, sortBy, stringDistance, tabulate, tabulate2D, throttle, toCamelCase, toLinkedList, toTitleCase, total, uid, unique, wait, words }; |
{ | ||
"name": "@mathigon/core", | ||
"version": "0.6.6", | ||
"version": "0.6.7", | ||
"description": "TypeScript utilities library containing function wrappers, string and array helper functions, event classes and color utilities.", | ||
@@ -32,13 +32,13 @@ "keywords": [ | ||
"@types/tape": "4.13.0", | ||
"@typescript-eslint/eslint-plugin": "4.20.0", | ||
"@typescript-eslint/parser": "4.20.0", | ||
"eslint": "7.23.0", | ||
"@typescript-eslint/eslint-plugin": "4.22.0", | ||
"@typescript-eslint/parser": "4.22.0", | ||
"eslint": "7.25.0", | ||
"eslint-config-google": "0.14.0", | ||
"eslint-plugin-import": "2.22.1", | ||
"rollup": "2.44.0", | ||
"rollup": "2.46.0", | ||
"tape": "5.2.2", | ||
"ts-node": "9.1.1", | ||
"tslib": "2.1.0", | ||
"typescript": "4.2.3" | ||
"tslib": "2.2.0", | ||
"typescript": "4.2.4" | ||
} | ||
} |
{ | ||
"extends": ["config:base"], | ||
"schedule": ["before 5am on the first day of the month"], | ||
"schedule": ["on the first day of the month"], | ||
"packageRules": [{ | ||
@@ -9,3 +9,3 @@ "packagePatterns": ["eslint"], | ||
}, { | ||
"packagePatterns": ["ts-node", "^typescript"], | ||
"packagePatterns": ["ts-node", "^typescript", "tslib"], | ||
"groupName": "typescript", | ||
@@ -12,0 +12,0 @@ "automerge": true |
@@ -61,2 +61,7 @@ // ============================================================================= | ||
/** Get the brightness of this colour. */ | ||
get brightness() { | ||
return (this.r * 299 + this.g * 587 + this.g * 114) / 1000; | ||
} | ||
/** Converts this colour to an HSL array. */ | ||
@@ -63,0 +68,0 @@ get hsl() { |
@@ -33,3 +33,28 @@ // ============================================================================= | ||
export function* pairs<S, T>(a: Iterable<S>, b: Iterable<T>) { | ||
for (const i of a) { | ||
for (const j of b) { | ||
yield [i, j]; | ||
} | ||
} | ||
} | ||
/** Find the item in an iterable for which value() returns the smallest value. */ | ||
export function findMin<T>(items: Iterable<T>, value: (item: T) => number, max = Infinity, min?: number) { | ||
let best: T|undefined = undefined; | ||
let v = max; | ||
for (const item of items) { | ||
const v1 = value(item); | ||
if (v1 < v) { | ||
best = item; | ||
v = v1; | ||
if (min !== undefined && v < min) return best; | ||
} | ||
} | ||
return best; | ||
} | ||
export class Itarray<T> implements Iterable<T> { | ||
@@ -36,0 +61,0 @@ private readonly values: Iterable<T>[]; |
@@ -31,3 +31,3 @@ // ============================================================================= | ||
/** Applies default keys to an object. */ | ||
export function applyDefaults(obj: any, defaults: any) { | ||
export function applyDefaults(obj: any = {}, defaults: any) { | ||
for (const key of Object.keys(defaults)) { | ||
@@ -91,2 +91,8 @@ if (!Object.prototype.hasOwnProperty.call(obj, key)) obj[key] = defaults[key]; | ||
class CacheError extends Error { | ||
constructor(readonly data: any) { | ||
super('[Cache Error]'); | ||
} | ||
} | ||
/** | ||
@@ -97,10 +103,18 @@ * Function wrapper that modifies a function to cache its return values. This | ||
* which are always called with different arguments. Note that argument | ||
* comparison doesn't not work with Objects or nested arrays. | ||
* comparison does not work with Objects or nested arrays. | ||
*/ | ||
export function cache<T>(fn: (...args: any[]) => T) { | ||
const cached = new Map<string, T>(); | ||
const cached = new Map<string, T|CacheError>(); | ||
return function(...args: any[]) { | ||
const argString = args.join('--'); | ||
if (!cached.has(argString)) cached.set(argString, fn(...args)); | ||
return cached.get(argString)!; | ||
if (!cached.has(argString)) { | ||
try { | ||
cached.set(argString, fn(...args)); | ||
} catch (e) { | ||
cached.set(argString, new CacheError(e)); | ||
} | ||
} | ||
const value = cached.get(argString)!; | ||
if (value instanceof CacheError) throw value.data; | ||
return value; | ||
}; | ||
@@ -107,0 +121,0 @@ } |
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
82253
2359