@mathigon/core
Advanced tools
Comparing version 0.6.7 to 0.6.8
@@ -274,10 +274,33 @@ 'use strict'; | ||
/** Converts an array to a linked list data structure. */ | ||
function toLinkedList(array) { | ||
const result = array.map(a => ({ val: a, next: undefined })); | ||
const n = result.length; | ||
for (let i = 0; i < n - 1; ++i) { | ||
result[i].next = result[i + 1]; | ||
class LinkedList { | ||
constructor(items) { | ||
const n = items.length; | ||
const mapped = items.map((val) => ({ val })); | ||
for (const [i, m] of mapped.entries()) { | ||
m.next = mapped[(i + 1) % n]; | ||
m.prev = mapped[(i - 1 + n) % n]; | ||
} | ||
this.root = mapped[0]; | ||
} | ||
result[n - 1].next = result[0]; | ||
return result; | ||
*traverse() { | ||
let current = this.root; | ||
while (current) { | ||
yield current; | ||
current = current.next; | ||
if (current === this.root) | ||
return; | ||
} | ||
} | ||
get array() { | ||
return Array.from(this.traverse()); | ||
} | ||
delete(node) { | ||
if (node === this.root) { | ||
if (node.next === node) | ||
return this.root = undefined; | ||
this.root = node.next; | ||
} | ||
node.prev.next = node.next; | ||
node.next.prev = node.prev; | ||
} | ||
} | ||
@@ -377,3 +400,3 @@ | ||
const shortHexRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; | ||
const longHexRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i; | ||
const longHexRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i; | ||
const rgbaRegex = /rgba?\(([0-9,]+), ?([0-9,]+), ?([0-9,]+)(, ?([0-9,]+))?\)/; | ||
@@ -419,3 +442,4 @@ const rainbow = ['#22ab24', '#009ea6', '#0f82f2', '#6d3bbf', | ||
const c = [this.r, this.g, this.b].map(x => pad2(Math.round(x).toString(16))); | ||
return '#' + c.join(''); | ||
const alpha = this.a >= 1 ? '' : pad2(Math.round(this.a * 255).toString(16)); | ||
return '#' + c.join('') + alpha; | ||
} | ||
@@ -474,9 +498,7 @@ /** Converts this colour to an rgba string. */ | ||
static fromHex(hex) { | ||
hex = hex.replace(shortHexRegex, function (m, r, g, b) { | ||
return r + r + g + g + b + b; | ||
}); | ||
hex = hex.replace(shortHexRegex, (m, r, g, b) => r + r + g + g + b + b); | ||
const rgbParts = longHexRegex.exec(hex); | ||
if (!rgbParts) | ||
return new Color(0, 0, 0); | ||
return new Color(parseInt(rgbParts[1], 16), parseInt(rgbParts[2], 16), parseInt(rgbParts[3], 16)); | ||
return new Color(parseInt(rgbParts[1], 16), parseInt(rgbParts[2], 16), parseInt(rgbParts[3], 16), rgbParts[4] ? parseInt(rgbParts[4], 16) / 255 : 1); | ||
} | ||
@@ -733,2 +755,3 @@ static fromHsl(h, s, l) { | ||
exports.Itarray = Itarray; | ||
exports.LinkedList = LinkedList; | ||
exports.applyDefaults = applyDefaults; | ||
@@ -768,3 +791,2 @@ exports.autoCorrect = autoCorrect; | ||
exports.toCamelCase = toCamelCase; | ||
exports.toLinkedList = toLinkedList; | ||
exports.toTitleCase = toTitleCase; | ||
@@ -771,0 +793,0 @@ exports.total = total; |
@@ -270,10 +270,33 @@ // ============================================================================= | ||
/** Converts an array to a linked list data structure. */ | ||
function toLinkedList(array) { | ||
const result = array.map(a => ({ val: a, next: undefined })); | ||
const n = result.length; | ||
for (let i = 0; i < n - 1; ++i) { | ||
result[i].next = result[i + 1]; | ||
class LinkedList { | ||
constructor(items) { | ||
const n = items.length; | ||
const mapped = items.map((val) => ({ val })); | ||
for (const [i, m] of mapped.entries()) { | ||
m.next = mapped[(i + 1) % n]; | ||
m.prev = mapped[(i - 1 + n) % n]; | ||
} | ||
this.root = mapped[0]; | ||
} | ||
result[n - 1].next = result[0]; | ||
return result; | ||
*traverse() { | ||
let current = this.root; | ||
while (current) { | ||
yield current; | ||
current = current.next; | ||
if (current === this.root) | ||
return; | ||
} | ||
} | ||
get array() { | ||
return Array.from(this.traverse()); | ||
} | ||
delete(node) { | ||
if (node === this.root) { | ||
if (node.next === node) | ||
return this.root = undefined; | ||
this.root = node.next; | ||
} | ||
node.prev.next = node.next; | ||
node.next.prev = node.prev; | ||
} | ||
} | ||
@@ -373,3 +396,3 @@ | ||
const shortHexRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; | ||
const longHexRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i; | ||
const longHexRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i; | ||
const rgbaRegex = /rgba?\(([0-9,]+), ?([0-9,]+), ?([0-9,]+)(, ?([0-9,]+))?\)/; | ||
@@ -415,3 +438,4 @@ const rainbow = ['#22ab24', '#009ea6', '#0f82f2', '#6d3bbf', | ||
const c = [this.r, this.g, this.b].map(x => pad2(Math.round(x).toString(16))); | ||
return '#' + c.join(''); | ||
const alpha = this.a >= 1 ? '' : pad2(Math.round(this.a * 255).toString(16)); | ||
return '#' + c.join('') + alpha; | ||
} | ||
@@ -470,9 +494,7 @@ /** Converts this colour to an rgba string. */ | ||
static fromHex(hex) { | ||
hex = hex.replace(shortHexRegex, function (m, r, g, b) { | ||
return r + r + g + g + b + b; | ||
}); | ||
hex = hex.replace(shortHexRegex, (m, r, g, b) => r + r + g + g + b + b); | ||
const rgbParts = longHexRegex.exec(hex); | ||
if (!rgbParts) | ||
return new Color(0, 0, 0); | ||
return new Color(parseInt(rgbParts[1], 16), parseInt(rgbParts[2], 16), parseInt(rgbParts[3], 16)); | ||
return new Color(parseInt(rgbParts[1], 16), parseInt(rgbParts[2], 16), parseInt(rgbParts[3], 16), rgbParts[4] ? parseInt(rgbParts[4], 16) / 255 : 1); | ||
} | ||
@@ -725,2 +747,2 @@ static fromHsl(h, s, l) { | ||
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 }; | ||
export { Cache, Color, EventTarget, Itarray, LinkedList, 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, toTitleCase, total, uid, unique, wait, words }; |
{ | ||
"name": "@mathigon/core", | ||
"version": "0.6.7", | ||
"version": "0.6.8", | ||
"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.22.0", | ||
"@typescript-eslint/parser": "4.22.0", | ||
"eslint": "7.25.0", | ||
"@typescript-eslint/eslint-plugin": "4.28.1", | ||
"@typescript-eslint/parser": "4.28.1", | ||
"eslint": "7.29.0", | ||
"eslint-config-google": "0.14.0", | ||
"eslint-plugin-import": "2.22.1", | ||
"rollup": "2.46.0", | ||
"eslint-plugin-import": "2.23.4", | ||
"rollup": "2.52.6", | ||
"tape": "5.2.2", | ||
"ts-node": "9.1.1", | ||
"tslib": "2.2.0", | ||
"typescript": "4.2.4" | ||
"ts-node": "10.0.0", | ||
"tslib": "2.3.0", | ||
"typescript": "4.3.5" | ||
} | ||
} |
@@ -161,19 +161,39 @@ // ============================================================================= | ||
type LinkedListItem<T> = {val: T, prev: LinkedListItem<T>, next: LinkedListItem<T>}; | ||
export interface LinkedListItem<T> { | ||
val: T; | ||
next?: LinkedListItem<T>; | ||
} | ||
/** Converts an array to a linked list data structure. */ | ||
export function toLinkedList<T>(array: T[]): LinkedListItem<T>[] { | ||
const result: LinkedListItem<T>[] = array.map(a => ({val: a, next: undefined})); | ||
export class LinkedList<T> { | ||
root?: LinkedListItem<T>; | ||
const n = result.length; | ||
for (let i = 0; i < n - 1; ++i) { | ||
result[i].next = result[i + 1]; | ||
constructor(items: T[]) { | ||
const n = items.length; | ||
const mapped = items.map((val) => ({val} as Partial<LinkedListItem<T>>)); | ||
for (const [i, m] of mapped.entries()) { | ||
m.next = mapped[(i + 1) % n] as LinkedListItem<T>; | ||
m.prev = mapped[(i - 1 + n) % n] as LinkedListItem<T>; | ||
} | ||
this.root = mapped[0] as LinkedListItem<T>; | ||
} | ||
result[n - 1].next = result[0]; | ||
return result; | ||
private* traverse() { | ||
let current = this.root; | ||
while (current) { | ||
yield current; | ||
current = current.next; | ||
if (current === this.root) return; | ||
} | ||
} | ||
get array() { | ||
return Array.from(this.traverse()); | ||
} | ||
delete(node: LinkedListItem<T>) { | ||
if (node === this.root) { | ||
if (node.next === node) return this.root = undefined; | ||
this.root = node.next; | ||
} | ||
node.prev.next = node.next; | ||
node.next.prev = node.prev; | ||
} | ||
} |
@@ -11,3 +11,3 @@ // ============================================================================= | ||
const shortHexRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; | ||
const longHexRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i; | ||
const longHexRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i; | ||
const rgbaRegex = /rgba?\(([0-9,]+), ?([0-9,]+), ?([0-9,]+)(, ?([0-9,]+))?\)/; | ||
@@ -51,5 +51,5 @@ | ||
get hex() { | ||
const c = [this.r, this.g, this.b].map( | ||
x => pad2(Math.round(x).toString(16))); | ||
return '#' + c.join(''); | ||
const c = [this.r, this.g, this.b].map(x => pad2(Math.round(x).toString(16))); | ||
const alpha = this.a >= 1 ? '' : pad2(Math.round(this.a * 255).toString(16)); | ||
return '#' + c.join('') + alpha; | ||
} | ||
@@ -120,5 +120,3 @@ | ||
static fromHex(hex: string) { | ||
hex = hex.replace(shortHexRegex, function(m, r, g, b) { | ||
return r + r + g + g + b + b; | ||
}); | ||
hex = hex.replace(shortHexRegex, (m, r, g, b) => r + r + g + g + b + b); | ||
@@ -131,3 +129,4 @@ const rgbParts = longHexRegex.exec(hex); | ||
parseInt(rgbParts[2], 16), | ||
parseInt(rgbParts[3], 16) | ||
parseInt(rgbParts[3], 16), | ||
rgbParts[4] ? parseInt(rgbParts[4], 16) / 255 : 1 | ||
); | ||
@@ -134,0 +133,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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
84385
2421
2426
1