@kaciras/utilities
Advanced tools
Comparing version 0.6.1 to 0.6.2
@@ -12,3 +12,3 @@ const htmlEscapes = { | ||
* | ||
* <h2>Alternatives</h2> | ||
* # Alternatives | ||
* To insert text into a DOM node, just assign `textContent`. | ||
@@ -74,4 +74,6 @@ */ function escapeHTML(html) { | ||
/** | ||
* The SingleEventEmitter calls all listeners synchronously in the | ||
* order in which they were registered. | ||
* Event dispatcher for only one type of event. | ||
* | ||
* Listeners are called synchronously in the order in which | ||
* they were registered. | ||
*/ class SingleEventEmitter { | ||
@@ -111,7 +113,6 @@ handlers = []; | ||
/** | ||
* # Alternatives | ||
* [nanoevents](https://github.com/ai/nanoevents). | ||
* | ||
* | ||
* <h2>Alternatives</h2> | ||
* In Node, you can import EventEmitter from "event" instead. | ||
* An only 152 KB library https://github.com/ai/nanoevents. | ||
*/ class MultiEventEmitter { | ||
@@ -155,28 +156,2 @@ events = Object.create(null); | ||
var TimeUnit; | ||
(function(TimeUnit) { | ||
TimeUnit[TimeUnit["NanoSecond"] = 0] = "NanoSecond"; | ||
TimeUnit[TimeUnit["MicroSecond"] = 2] = "MicroSecond"; | ||
TimeUnit[TimeUnit["MilliSecond"] = 4] = "MilliSecond"; | ||
TimeUnit[TimeUnit["Second"] = 6] = "Second"; | ||
TimeUnit[TimeUnit["Minute"] = 8] = "Minute"; | ||
TimeUnit[TimeUnit["Hour"] = 10] = "Hour"; | ||
TimeUnit[TimeUnit["Day"] = 12] = "Day"; | ||
})(TimeUnit || (TimeUnit = {})); | ||
const TIME_UNITS = [ | ||
"ns", | ||
1000, | ||
"us", | ||
1000, | ||
"ms", | ||
1000, | ||
"s", | ||
60, | ||
"m", | ||
60, | ||
"h", | ||
24, | ||
"d", | ||
Infinity | ||
]; | ||
const SIZE_UNITS_SI = [ | ||
@@ -240,14 +215,51 @@ "", | ||
const [, v, unit = ""] = match; | ||
let sss = Number(v); | ||
let result = Number(v); | ||
for(let i = 0; i < units.length; i += 2){ | ||
if (units[i] === unit) { | ||
return sss; | ||
return result; | ||
} else { | ||
sss *= units[i + 1]; | ||
result *= units[i + 1]; | ||
} | ||
} | ||
throw new Error("Unknown unit: " + unit); | ||
throw new Error(`Unknown unit: ${unit}B`); | ||
} | ||
function formatDuration(value, unit) { | ||
return toString(value, TIME_UNITS, unit); | ||
const TIME_UNITS = [ | ||
"ns", | ||
1000, | ||
"us", | ||
1000, | ||
"ms", | ||
1000, | ||
"s", | ||
60, | ||
"m", | ||
60, | ||
"h", | ||
24, | ||
"d", | ||
Infinity | ||
]; | ||
function formatDuration(value, unit, parts = 2) { | ||
let i = TIME_UNITS.indexOf(unit); | ||
let d = 1; | ||
if (i === -1) { | ||
throw new Error(`Unknown time unit: ${unit}`); | ||
} | ||
for(;; i += 2){ | ||
const x = d * TIME_UNITS[i + 1]; | ||
if (value < x) break; | ||
else d = x; | ||
} | ||
const groups = []; | ||
// 1.16e-14 = 1/24/60/60/1000... | ||
for(; i >= 0 && parts > 0 && value > 1.16e-14; i -= 2, parts -= 1){ | ||
const t = Math.floor(value / d); | ||
// Avoid leading zeros. | ||
if (groups.length || t !== 0) { | ||
groups.push(`${t}${TIME_UNITS[i]}`); | ||
} | ||
value %= d; | ||
d /= TIME_UNITS[i - 1]; | ||
} | ||
return groups.length ? groups.join(" ") : `0${unit}`; | ||
} | ||
@@ -277,2 +289,5 @@ /** | ||
* | ||
* # Alternatives | ||
* [mustache.js](https://github.com/janl/mustache.js) | ||
* | ||
* @example | ||
@@ -287,5 +302,5 @@ * const template = "<html>...</html>"; | ||
* const c = newComposite(); | ||
* c.put("appHtml", appHtml); | ||
* c.put("metadata", meta); | ||
* c.put("bodyAttrs", ` class="${bodyClass}"`); | ||
* c.put("appHtml", "<div id='app'>...</div>"); | ||
* c.put("metadata", "<meta...>"); | ||
* c.put("bodyAttrs", " class='ssr dark'"); | ||
* return composite.toString(); | ||
@@ -438,3 +453,3 @@ * | ||
/** | ||
* A very simple helper to make `fetch` simpler. | ||
* A very simple helper to make `fetch` easier. | ||
* | ||
@@ -466,3 +481,3 @@ * # Alternatives | ||
}; | ||
// fetch will auto set content-type if body is some types. | ||
// fetch will set content-type if body is some types. | ||
if (data instanceof FormData) { | ||
@@ -474,3 +489,3 @@ custom.body = data; | ||
} | ||
const request = new Request(new URL(url, baseURL), init); | ||
const request = new Request(baseURL + url, init); | ||
return new ResponseFacade(fetch(request, custom), check); | ||
@@ -690,3 +705,3 @@ } | ||
* | ||
* <h1>Limitation</h1> | ||
* # Limitation | ||
* There is no way to detect the user clicked the cancel button, | ||
@@ -777,3 +792,3 @@ * if the user does,the returned Promise will never resolve. | ||
* | ||
* <h2>NOTE</h2> | ||
* # NOTE | ||
* If you disable timeout, there will be a memory leak when response | ||
@@ -851,2 +866,7 @@ * message can't be received. | ||
* TODO: benchmark against https://github.com/dominictarr/hashlru | ||
* | ||
* # Alternatives | ||
* [lru-cache](https://github.com/isaacs/node-lru-cache) | ||
* [Mnemonist is a curated collection of data structures](https://github.com/yomguithereal/mnemonist) | ||
* [quick-lru](https://github.com/sindresorhus/quick-lru) | ||
*/ class LRUCache { | ||
@@ -955,2 +975,2 @@ map = new Map(); | ||
export { AbortError, Composite, FetchClient, FetchClientError, LRUCache, MultiEventEmitter, MultiMap, NeverAbort, rpc as RPC, ResponseFacade, SingleEventEmitter, TimeUnit, base64url, blobToBase64URL, compositor, dragSortContext, escapeHTML, fetchFile, formatDuration, formatSize, identity, noop, parseSize, saveFile, selectFile, sha256, silencePromise, sleep, svgToUrl, swapElements, uniqueId }; | ||
export { AbortError, Composite, FetchClient, FetchClientError, LRUCache, MultiEventEmitter, MultiMap, NeverAbort, rpc as RPC, ResponseFacade, SingleEventEmitter, base64url, blobToBase64URL, compositor, dragSortContext, escapeHTML, fetchFile, formatDuration, formatSize, identity, noop, parseSize, saveFile, selectFile, sha256, silencePromise, sleep, svgToUrl, swapElements, uniqueId }; |
@@ -6,3 +6,3 @@ /// <reference types="node" resolution-mode="require"/> | ||
* | ||
* <h2>Alternatives</h2> | ||
* # Alternatives | ||
* To insert text into a DOM node, just assign `textContent`. | ||
@@ -9,0 +9,0 @@ */ |
type Handler<T extends any[]> = (...args: T) => any; | ||
/** | ||
* The SingleEventEmitter calls all listeners synchronously in the | ||
* order in which they were registered. | ||
* Event dispatcher for only one type of event. | ||
* | ||
* Listeners are called synchronously in the order in which | ||
* they were registered. | ||
*/ | ||
@@ -30,7 +32,6 @@ export declare class SingleEventEmitter<T extends any[] = any[]> { | ||
/** | ||
* # Alternatives | ||
* [nanoevents](https://github.com/ai/nanoevents). | ||
* | ||
* | ||
* <h2>Alternatives</h2> | ||
* In Node, you can import EventEmitter from "event" instead. | ||
* An only 152 KB library https://github.com/ai/nanoevents. | ||
*/ | ||
@@ -37,0 +38,0 @@ export declare class MultiEventEmitter<T extends EventsMap = Default> { |
@@ -55,3 +55,3 @@ /** | ||
/** | ||
* A very simple helper to make `fetch` simpler. | ||
* A very simple helper to make `fetch` easier. | ||
* | ||
@@ -58,0 +58,0 @@ * # Alternatives |
@@ -1,11 +0,3 @@ | ||
export declare enum TimeUnit { | ||
NanoSecond = 0, | ||
MicroSecond = 2, | ||
MilliSecond = 4, | ||
Second = 6, | ||
Minute = 8, | ||
Hour = 10, | ||
Day = 12 | ||
} | ||
export declare function formatDuration(value: number, unit: TimeUnit): string; | ||
type TimeUnit = "ns" | "ms" | "s" | "m" | "h" | "d"; | ||
export declare function formatDuration(value: number, unit: TimeUnit, parts?: number): string; | ||
/** | ||
@@ -33,2 +25,5 @@ * Convert bytes to a human-readable string. | ||
* | ||
* # Alternatives | ||
* [mustache.js](https://github.com/janl/mustache.js) | ||
* | ||
* @example | ||
@@ -43,5 +38,5 @@ * const template = "<html>...</html>"; | ||
* const c = newComposite(); | ||
* c.put("appHtml", appHtml); | ||
* c.put("metadata", meta); | ||
* c.put("bodyAttrs", ` class="${bodyClass}"`); | ||
* c.put("appHtml", "<div id='app'>...</div>"); | ||
* c.put("metadata", "<meta...>"); | ||
* c.put("bodyAttrs", " class='ssr dark'"); | ||
* return composite.toString(); | ||
@@ -48,0 +43,0 @@ * |
@@ -12,3 +12,3 @@ /** | ||
* | ||
* <h1>Limitation</h1> | ||
* # Limitation | ||
* There is no way to detect the user clicked the cancel button, | ||
@@ -15,0 +15,0 @@ * if the user does,the returned Promise will never resolve. |
@@ -24,2 +24,7 @@ type Dispose<T> = (value: T) => unknown; | ||
* TODO: benchmark against https://github.com/dominictarr/hashlru | ||
* | ||
* # Alternatives | ||
* [lru-cache](https://github.com/isaacs/node-lru-cache) | ||
* [Mnemonist is a curated collection of data structures](https://github.com/yomguithereal/mnemonist) | ||
* [quick-lru](https://github.com/sindresorhus/quick-lru) | ||
*/ | ||
@@ -26,0 +31,0 @@ export default class LRUCache<K, T> { |
@@ -17,4 +17,7 @@ /// <reference types="node" resolution-mode="require"/> | ||
* | ||
* # Alternatives | ||
* [node-graceful](https://github.com/mrbar42/node-graceful) | ||
* | ||
* @param listener listener function | ||
*/ | ||
export declare function onExit(listener: (signal: Signals) => any): void; |
115
dist/node.js
@@ -16,3 +16,3 @@ import process from 'process'; | ||
* | ||
* <h2>Alternatives</h2> | ||
* # Alternatives | ||
* To insert text into a DOM node, just assign `textContent`. | ||
@@ -79,4 +79,6 @@ */ function escapeHTML(html) { | ||
/** | ||
* The SingleEventEmitter calls all listeners synchronously in the | ||
* order in which they were registered. | ||
* Event dispatcher for only one type of event. | ||
* | ||
* Listeners are called synchronously in the order in which | ||
* they were registered. | ||
*/ class SingleEventEmitter { | ||
@@ -116,7 +118,6 @@ handlers = []; | ||
/** | ||
* # Alternatives | ||
* [nanoevents](https://github.com/ai/nanoevents). | ||
* | ||
* | ||
* <h2>Alternatives</h2> | ||
* In Node, you can import EventEmitter from "event" instead. | ||
* An only 152 KB library https://github.com/ai/nanoevents. | ||
*/ class MultiEventEmitter { | ||
@@ -245,3 +246,3 @@ events = Object.create(null); | ||
/** | ||
* A very simple helper to make `fetch` simpler. | ||
* A very simple helper to make `fetch` easier. | ||
* | ||
@@ -273,3 +274,3 @@ * # Alternatives | ||
}; | ||
// fetch will auto set content-type if body is some types. | ||
// fetch will set content-type if body is some types. | ||
if (data instanceof FormData) { | ||
@@ -281,3 +282,3 @@ custom.body = data; | ||
} | ||
const request = new Request(new URL(url, baseURL), init); | ||
const request = new Request(baseURL + url, init); | ||
return new ResponseFacade(fetch(request, custom), check); | ||
@@ -305,28 +306,2 @@ } | ||
var TimeUnit; | ||
(function(TimeUnit) { | ||
TimeUnit[TimeUnit["NanoSecond"] = 0] = "NanoSecond"; | ||
TimeUnit[TimeUnit["MicroSecond"] = 2] = "MicroSecond"; | ||
TimeUnit[TimeUnit["MilliSecond"] = 4] = "MilliSecond"; | ||
TimeUnit[TimeUnit["Second"] = 6] = "Second"; | ||
TimeUnit[TimeUnit["Minute"] = 8] = "Minute"; | ||
TimeUnit[TimeUnit["Hour"] = 10] = "Hour"; | ||
TimeUnit[TimeUnit["Day"] = 12] = "Day"; | ||
})(TimeUnit || (TimeUnit = {})); | ||
const TIME_UNITS = [ | ||
"ns", | ||
1000, | ||
"us", | ||
1000, | ||
"ms", | ||
1000, | ||
"s", | ||
60, | ||
"m", | ||
60, | ||
"h", | ||
24, | ||
"d", | ||
Infinity | ||
]; | ||
const SIZE_UNITS_SI = [ | ||
@@ -390,14 +365,51 @@ "", | ||
const [, v, unit = ""] = match; | ||
let sss = Number(v); | ||
let result = Number(v); | ||
for(let i = 0; i < units.length; i += 2){ | ||
if (units[i] === unit) { | ||
return sss; | ||
return result; | ||
} else { | ||
sss *= units[i + 1]; | ||
result *= units[i + 1]; | ||
} | ||
} | ||
throw new Error("Unknown unit: " + unit); | ||
throw new Error(`Unknown unit: ${unit}B`); | ||
} | ||
function formatDuration(value, unit) { | ||
return toString(value, TIME_UNITS, unit); | ||
const TIME_UNITS = [ | ||
"ns", | ||
1000, | ||
"us", | ||
1000, | ||
"ms", | ||
1000, | ||
"s", | ||
60, | ||
"m", | ||
60, | ||
"h", | ||
24, | ||
"d", | ||
Infinity | ||
]; | ||
function formatDuration(value, unit, parts = 2) { | ||
let i = TIME_UNITS.indexOf(unit); | ||
let d = 1; | ||
if (i === -1) { | ||
throw new Error(`Unknown time unit: ${unit}`); | ||
} | ||
for(;; i += 2){ | ||
const x = d * TIME_UNITS[i + 1]; | ||
if (value < x) break; | ||
else d = x; | ||
} | ||
const groups = []; | ||
// 1.16e-14 = 1/24/60/60/1000... | ||
for(; i >= 0 && parts > 0 && value > 1.16e-14; i -= 2, parts -= 1){ | ||
const t = Math.floor(value / d); | ||
// Avoid leading zeros. | ||
if (groups.length || t !== 0) { | ||
groups.push(`${t}${TIME_UNITS[i]}`); | ||
} | ||
value %= d; | ||
d /= TIME_UNITS[i - 1]; | ||
} | ||
return groups.length ? groups.join(" ") : `0${unit}`; | ||
} | ||
@@ -427,2 +439,5 @@ /** | ||
* | ||
* # Alternatives | ||
* [mustache.js](https://github.com/janl/mustache.js) | ||
* | ||
* @example | ||
@@ -437,5 +452,5 @@ * const template = "<html>...</html>"; | ||
* const c = newComposite(); | ||
* c.put("appHtml", appHtml); | ||
* c.put("metadata", meta); | ||
* c.put("bodyAttrs", ` class="${bodyClass}"`); | ||
* c.put("appHtml", "<div id='app'>...</div>"); | ||
* c.put("metadata", "<meta...>"); | ||
* c.put("bodyAttrs", " class='ssr dark'"); | ||
* return composite.toString(); | ||
@@ -662,3 +677,3 @@ * | ||
* | ||
* <h2>NOTE</h2> | ||
* # NOTE | ||
* If you disable timeout, there will be a memory leak when response | ||
@@ -737,2 +752,7 @@ * message can't be received. | ||
* TODO: benchmark against https://github.com/dominictarr/hashlru | ||
* | ||
* # Alternatives | ||
* [lru-cache](https://github.com/isaacs/node-lru-cache) | ||
* [Mnemonist is a curated collection of data structures](https://github.com/yomguithereal/mnemonist) | ||
* [quick-lru](https://github.com/sindresorhus/quick-lru) | ||
*/ class LRUCache { | ||
@@ -847,2 +867,5 @@ map = new Map(); | ||
* | ||
* # Alternatives | ||
* [node-graceful](https://github.com/mrbar42/node-graceful) | ||
* | ||
* @param listener listener function | ||
@@ -869,2 +892,2 @@ */ function onExit(listener) { | ||
export { AbortError, Composite, FetchClient, FetchClientError, LRUCache, MultiEventEmitter, MultiMap, NeverAbort, rpc as RPC, ResponseFacade, SingleEventEmitter, TimeUnit, base64url, blobToBase64URL, compositor, escapeHTML, fetchFile, formatDuration, formatSize, identity, noop, onExit, parseSize, sha256, silencePromise, sleep, svgToUrl, uniqueId }; | ||
export { AbortError, Composite, FetchClient, FetchClientError, LRUCache, MultiEventEmitter, MultiMap, NeverAbort, rpc as RPC, ResponseFacade, SingleEventEmitter, base64url, blobToBase64URL, compositor, escapeHTML, fetchFile, formatDuration, formatSize, identity, noop, onExit, parseSize, sha256, silencePromise, sleep, svgToUrl, uniqueId }; |
@@ -53,3 +53,3 @@ export type Respond = (resp: ResponseMessage, transfer?: Transferable[]) => void; | ||
* | ||
* <h2>NOTE</h2> | ||
* # NOTE | ||
* If you disable timeout, there will be a memory leak when response | ||
@@ -56,0 +56,0 @@ * message can't be received. |
{ | ||
"name": "@kaciras/utilities", | ||
"version": "0.6.1", | ||
"version": "0.6.2", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A set of common JS functions for node and browser.", |
72105
2290