
A collection of useful JavaScript helpers.
Demos
Sources
📄 File: src/deep_equal.js
Helper that verifies deeply equality of two arguments of any type.
Usage example from unit tests:
import { deep_equal } from '../src/deep_equal.js'
import { it } from '../src/it.js'
it('should work', () => {
deep_equal(true, true).should.be.true
deep_equal(true, false).should.be.false
deep_equal(false, true).should.be.false
deep_equal(false, false).should.be.true
deep_equal(null, null).should.be.true
deep_equal(null, undefined).should.be.false
deep_equal(undefined, null).should.be.false
deep_equal(undefined, undefined).should.be.true
deep_equal(42, 42).should.be.true
deep_equal(42, '42').should.be.false
deep_equal('42', 42).should.be.false
deep_equal('42', '42').should.be.true
deep_equal([1, { a: 2, b: 3 }, 4], [1, { b: 3, a: 2 }, 4]).should.be.true
deep_equal([1, { a: 2, b: 3 }, 4], [1, { b: 3, a: '2' }, 4]).should.be.false
deep_equal([1, { a: 2, b: 3 }, 4], [1, { b: 3, a: 2 }, '4']).should.be.false
deep_equal(Object.create(null), {}).should.be.true
deep_equal(() => { }, () => { }).should.be.false
const keys = 'abcdefghilklmnopqrstuvwxyz0123456789_$'.split('')
const a = [{}, {}, {}]
const t = [a[0], a[1], a[2]]
for (let i = 0; i < t.length; ++i) {
for (let k = 0; k < 10000; ++k) {
t[i] = t[i][keys[k % keys.length]] = {}
}
t[i].deepest = !i
}
deep_equal(a[0], a[1]).should.be.false
deep_equal(a[0], a[2]).should.be.false
deep_equal(a[1], a[2]).should.be.true
})
🟨 Function: deep_equal
function deep_equal(actual: any, expected: any): boolean;
Helper that verifies deeply equality of two arguments of any type.
An iterative implementation that does not cause a stack overflow exception.
🔴 Parameter: actual
any
🔴 Parameter: expected
any
🟢 Returns
boolean
📄 File: src/download.js
Helper for handling client-side (web browser) generated downloads.
Usage example from unit tests:
import { it } from '../src/it.js'
import { download } from '../src/download.js'
it('should work', () => {
download('Some content', 'name.txt', 'plain/text')
})
🟨 Function: download
function download(blobPart?: BlobPart[] | undefined, download?: string | undefined, type?: string | undefined): void;
Helper for handling client-side (web browser) generated downloads.
🔴 Parameter: blobPart
BlobPart[]=
🔴 Parameter: download
string=
🔴 Parameter: type
string=
📄 File: src/elvis.js
A set of helpers for working with objects and arrays.
Usage example from unit tests:
import { arr, get, obj, set } from '../src/elvis.js'
import { it } from '../src/it.js'
it('should create array of proper values', () => {
const actual = arr` ${'The answer'} to life the universe and everything:
${42}${true} `
const expected = [
'The answer', 'to', 'life', 'the', 'universe', 'and', 'everything:',
42, true
]
actual.should.be.deep.equal(expected)
})
it('should get existing non-nested property', () => {
const actual_1 = { a: 42 }
get(actual_1, 'a').should.be.equal(42)
})
it('should get existing nested property and get undefined for non existing nested property', () => {
const actual_2 = {
The: {
answer: {
to: {
life: {
the: {
universe: {
and: {
everything: 42
}
}
}
}
}
}
}
}
get(actual_2, ...arr`The answer to life the universe and everything`).should.be.equal(42)
expect(get(actual_2, ...arr`The answer to life the Universe and everything`)).to.be.undefined
})
it('should set not existing non-nested property', () => {
const actual_1 = {}
set(actual_1).a = 42
actual_1.should.be.deep.equal({ a: 42 })
})
it('shoud set not existing nested property', () => {
const actual_2 = {}
set(actual_2, ...arr`E l v i`).s = 42
actual_2.should.be.deep.equal({ E: { l: { v: { i: { s: 42 } } } } })
})
it('should create object of undefined values', () => {
const actual = obj` The answer to life the universe and everything:
${42} is greater than ${17} `
const expected = {
The: undefined,
answer: undefined,
to: undefined,
life: undefined,
the: undefined,
universe: undefined,
and: undefined,
'everything:': undefined,
42: undefined,
is: undefined,
greater: undefined,
than: undefined,
17: undefined
}
expect(actual).to.be.deep.equal(expected)
})
🟨 Function: arr
function arr(template: TemplateStringsArray, ...substitutions: any[]): any[];
Array from tagged template literal.
🔴 Parameter: template
TemplateStringsArray
🔴 Parameter: substitutions
any[]
🟢 Returns
any[]
🟨 Function: get
function get(ref: object, ...keys: string[]): object | undefined;
Helper similar to ? operator (Elvis operator) for easy accessing nested object values.
🔴 Parameter: ref
object
🔴 Parameter: keys
string[]
🟢 Returns
object=
🟨 Function: obj
function obj(params_0: TemplateStringsArray, ...params_1: any[]): {
[key: string]: undefined;
};
Object from tagged template literal.
🔴 Parameter: params
[TemplateStringsArray, ...any[]]
🟢 Returns
{
[key: string]: undefined;
}
🟨 Function: set
function set(ref: object, ...keys: string[]): object | undefined;
Helper similar to ? operator (Elvis operator) for easy assigning values to nested objects.
🔴 Parameter: ref
object
🔴 Parameter: keys
string[]
🟢 Returns
object=
📄 File: src/fix_typography.js
Helper implementing typographic corrections appropriate for Polish typography.
Usage example from unit tests:
import { j } from '../src/j.js'
import { fix_typography } from '../src/fix_typography.js'
import { it } from '../src/it.js'
it('should work', () => {
const p = j({
t: 'p',
k: {
innerHTML: 'a b c d e f g h i j k l m n o p q r s t u v w x y z ' +
'https://example.com ' +
'<a href="https://example.com">https://example.com</a>'
}
}).e
fix_typography(p)
p.outerHTML.replace(/\u200B/g, '​').should.be.equal(
'<p>a b c d e f g h ' +
'<span class="nbsp">i </span>j k l m n ' +
'<span class="nbsp">o </span>p q r s t ' +
'<span class="nbsp">u </span>v ' +
'<span class="nbsp">w </span>x y ' +
'<span class="nbsp">z </span>' +
'https:/​/​example.​com ' +
'<a href="https://example.com">' +
'https:/​/​example.​com' +
'</a></p>'
)
})
🟨 Function: fix_typography
function fix_typography(htmlElement: HTMLElement): void;
Helper implementing typographic corrections appropriate for Polish typography.
An iterative implementation that does not cause a stack overflow exception.
🔴 Parameter: htmlElement
HTMLElement
📄 File: src/fts.js
A set of helpers for Full-Text Search.
Usage example from unit tests:
import { substitution_costs_default, fts_index, fts_rank, levenshtein_distance, substitution_costs_pl } from '../src/fts.js'
import { it } from '../src/it.js'
it('should work', () => {
const count_word_id = {}
const count_word = {}
const count_id = {}
const index = fts_index.bind(0, count_word_id, count_word, count_id)
const rank = fts_rank.bind(0, count_word_id, count_word, count_id)
index('one', 1)
index('one', 2)
index('two', 2)
index('two', 3)
rank('one').should.be.deep.equal({ 1: 0.5, 2: 0.25 })
rank('two').should.be.deep.equal({ 2: 0.25, 3: 0.5 })
})
it('should correctly calculate the Levenshtein distance', () => {
const levenshtein = levenshtein_distance.bind(null, 1, 1, substitution_costs_default)
levenshtein('kot', 'kat').should.be.equal(1)
levenshtein('koty', 'kat').should.be.equal(2)
levenshtein('levenshtein', 'lewensztejn').should.be.equal(3)
levenshtein('kitten', 'sitting').should.be.equal(3)
levenshtein('kat', 'kąt').should.be.equal(1)
const levenshtein_pl = levenshtein_distance.bind(null, 1, 1, substitution_costs_pl)
levenshtein_pl('kat', 'kąt').should.be.equal(0.3)
})
🟩 Type: SUBSTITUTION_COST
type SUBSTITUTION_COST = (letter_1: string, letter_2: string) => number;
The substitution cost function used by levenshtein_distance.
🔴 Parameter: letter_1
string
🔴 Parameter: letter_2
string
🟢 Returns
number
🟨 Function: fts_index
function fts_index(count_word_id: {
[word: string]: {
[id: string]: number;
};
}, count_word: {
[word: string]: number;
}, count_id: {
[id: string]: number;
}, word: string, id: string, rank?: number | undefined): void;
Helper that indexes the specified word within the specified id.
🔴 Parameter: count_word_id
{
[word: string]: {
[id: string]: number;
};
}
🔴 Parameter: count_word
{
[word: string]: number;
}
🔴 Parameter: count_id
{
[id: string]: number;
}
🔴 Parameter: word
string
🔴 Parameter: id
string
🔴 Parameter: rank
number=
🟨 Function: fts_init_counters
function fts_init_counters(count_word_id: {
[word: string]: {
[id: string]: number;
};
}): [{
[word: string]: number;
}, {
[id: string]: number;
}];
Helper that creates count_word and count_id maps based on the count_word_id map.
🔴 Parameter: count_word_id
{
[word: string]: {
[id: string]: number;
};
}
🟢 Returns
[{
[word: string]: number;
}, {
[id: string]: number;
}]
🟨 Function: fts_rank
function fts_rank(count_word_id: {
[word: string]: {
[id: string]: number;
};
}, count_word: {
[word: string]: number;
}, count_id: {
[id: string]: number;
}, word: string, result?: {
[id: string]: number;
}): {
[id: string]: number;
};
Helper that searches for the given word among indexed words.
Returns a map of non-zero relevance coefficients for registered identifiers.
🔴 Parameter: count_word_id
{
[word: string]: {
[id: string]: number;
};
}
🔴 Parameter: count_word
{
[word: string]: number;
}
🔴 Parameter: count_id
{
[id: string]: number;
}
🔴 Parameter: word
string
🔴 Parameter: result
{
[id: string]: number;
}=
🟢 Returns
{
[id: string]: number;
}
🟨 Function: levenshtein_distance
function levenshtein_distance(deletion_cost: number, insertion_cost: number, substitution_cost: SUBSTITUTION_COST, word_1: string, word_2: string): number;
Helper for calculating Levenshtein distances.
🔴 Parameter: deletion_cost
number
🔴 Parameter: insertion_cost
number
🔴 Parameter: substitution_cost
SUBSTITUTION_COST
🔴 Parameter: word_1
string
🔴 Parameter: word_2
string
🟢 Returns
number
🟥 Constant: substitution_costs_default
const substitution_costs_default: SUBSTITUTION_COST;
Default substitution costs function to use with levenshtein_distance.
🟠 Type
SUBSTITUTION_COST
🟥 Constant: substitution_costs_pl
const substitution_costs_pl: SUBSTITUTION_COST;
Substitution costs function for the Polish language (to use with levenshtein_distance).
🟠 Type
SUBSTITUTION_COST
📄 File: src/is_in.js
Replacement for the in operator (not to be confused with the for-in loop) that works properly.
Usage example from unit tests:
import { it } from '../src/it.js'
import { is_in } from '../src/is_in.js'
it('should work', () => {
const ob = { key: 'K', null: 'N' }
expect('key' in ob).to.be.true
is_in('key', ob).should.be.true
expect('null' in ob).to.be.true
is_in('null', ob).should.be.true
expect(null in ob).to.be.true
is_in(null, ob).should.be.false
expect('toString' in ob).to.be.true
is_in('toString', ob).should.be.false
is_in('key', null).should.be.false
})
🟨 Function: is_in
function is_in(key: any, map: any): boolean;
Replacement for the in operator (not to be confused with the for-in loop) that works properly.
🔴 Parameter: key
any
🔴 Parameter: map
any
🟢 Returns
boolean
📄 File: src/it.js
A simple helper for various types of tests (especially unit tests).
🟨 Function: it
function it(message: string, callback: Function): Promise<void>;
A simple helper for various types of tests (especially unit tests).
🔴 Parameter: message
string
🔴 Parameter: callback
function
🟨 Function: summary
function summary(): number;
Prints a summary of the tests.
🟢 Returns
number
📄 File: src/j.js
Lightweight helper for creating and modifying DOM elements.
Usage example from unit tests:
import { it } from '../src/it.js'
import { j, j_style, j_svg, j_svg_use, j_symbol, toe } from '../src/j.js'
it('should create <b><i> with class attribute and textContent', () => {
j({
t: 'b',
i: [{
t: 'i', k: { className: 'some class', textContent: 'text' }
}]
}).e.outerHTML.should.be.equal('<b><i class="some class">text</i></b>')
})
const b = j({ t: 'b' })
it('should create empty <b>', () => {
b.e.outerHTML.should.be.equal('<b></b>')
})
const i = j({
t: 'i', i: [{ e: 'text' }], p: b.e
})
it('should create <i> with previous <b> as a child', () => {
i.e.outerHTML.should.be.equal('<i>text</i>')
b.e.outerHTML.should.be.equal('<b><i>text</i></b>')
})
j({
e: i.e, k: { className: 'some class' }
})
it('should add some class to previous <i> and previous <b>', () => {
i.e.outerHTML.should.be.equal('<i class="some class">text</i>')
b.e.outerHTML.should.be.equal('<b><i class="some class">text</i></b>')
})
it('should set textContent by child node', () => {
j({
t: 'span', k: { className: 'some class' }, i: [{ e: 0 }]
}).e.outerHTML.should.be.equal('<span class="some class">0</span>')
})
it('should set textContent by textContent property', () => {
j({
t: 'span', k: { className: 'some class', textContent: 'one' }
}).e.outerHTML.should.be.equal('<span class="some class">one</span>')
})
it('should set style by style properties', () => {
j({
t: 'div',
k: {
style: { margin: 0 }
}
}).e.outerHTML.should.be.equal('<div style="margin: 0px;"></div>')
})
it('should create <div> with child nodes', () => {
j({
t: 'div',
k: { className: 'some class' },
i: [{
t: 'b', i: [{ e: 'bold 1' }]
}, {
t: 'b', i: [{ e: 'bold 2' }]
}, {
t: 'i', i: [{ e: 'italic' }]
}]
}).e.outerHTML.should.be.equal(
'<div class="some class">' +
'<b>bold 1</b><b>bold 2</b><i>italic</i>' +
'</div>')
})
const input_1 = j({
t: 'input', k: { value: 42 }
})
const input_2 = j({
t: 'input', a: { value: 42 }
})
it('should set <input> value set by value property', () => {
input_1.e.value.should.be.equal('42')
})
it('should set <input> value set by value attribute', () => {
input_2.e.value.should.be.equal('42')
})
it('should not expose <input> value attribute set by value property', () => {
input_1.e.outerHTML.should.be.equal('<input>')
})
it('should expose <input> value attribute set by value attribute', () => {
input_2.e.outerHTML.should.be.equal('<input value="42">')
})
it('should wrap the argument to an object with a single property ‘e’', () => {
toe(42).should.be.deep.equal({ e: 42 })
})
it('should handle boolean attributes in a special way', () => {
j({
t: 'input', a: { type: 'checkbox', checked: true }
}).e.outerHTML.should.be.equal('<input type="checkbox" checked="">')
j({
t: 'input', a: { type: 'checkbox', checked: false }
}).e.outerHTML.should.be.equal('<input type="checkbox">')
j({
t: 'input', a: { type: 'checkbox', 'xhtml:checked': true }
}).e.outerHTML.should.be.equal('<input type="checkbox" checked="">')
j({
t: 'input', a: { type: 'checkbox', 'xhtml:checked': false }
}).e.outerHTML.should.be.equal('<input type="checkbox">')
})
it('should assign the property by reference if assignment by subkeys is not possible', () => {
j({
t: 'div', k: { key: { key_2: 42 } }
}).e.key.should.be.deep.equal({ key_2: 42 })
})
it('should work', () => {
j_symbol({}).e.outerHTML.should.be.equal('<symbol></symbol>')
j_symbol({ t: 'non-symbol' }).e.outerHTML.should.be.equal('<symbol></symbol>')
j_symbol({ a: { id: 'id' } }).e.outerHTML.should.be.equal('<symbol id="id"></symbol>')
})
it('should work', () => {
j_svg([{
a: { viewBox: '0 1 2 3', id: 'id' },
i: [{ t: 'some-tag' }]
}]).e.outerHTML.should.be.equal(
'<svg style="display: none;">' +
'<symbol viewBox="0 1 2 3" id="id"><some-tag></some-tag></symbol>' +
'</svg>')
})
it('should handle id', () => {
j(j_svg_use('id')).e.outerHTML.replace('xlink:', '').should.be.equal(
'<svg><use href="#id"></use></svg>')
})
it('should handle extra attributes', () => {
j({
...j_svg_use('id'),
a: { fill: '#000', stroke: '#000', width: 42, height: 42 }
}).e.outerHTML.replace('xlink:', '').should.be.equal(
'<svg fill="#000" stroke="#000" width="42" height="42">' +
'<use href="#id"></use>' +
'</svg>')
})
it('should work', () => {
const js_style = {
a: { b$$1: 1, b$$2: 2 }
}
j_style(js_style).e.outerHTML.should.be.equal(
'<style>a{b:1;b:2}</style>')
j_style(js_style, '$$$').e.outerHTML.should.be.equal(
'<style>a{b$$1:1;b$$2:2}</style>')
})
🟩 Type: E
type E<T> = {
e: T;
};
Result type of the j helper.
🔵 Template parameter: T
🟡 Property: e
T
🟩 Type: J_CONFIG
type J_CONFIG = {
a?: {
[attribute: string]: any;
};
e?: (InstanceType<Element> | Text | string | number) | undefined;
i?: J_CONFIG[] | undefined;
k?: {
[key: string]: any;
};
n?: string | undefined;
p?: InstanceType<Element> | undefined;
t?: string | undefined;
};
The j helper configuration.
🟡 Property: a
{
[attribute: string]: any;
}=
attributes of the created or modified element set by setAttribute or setAttributeNS
🟡 Property: e
(InstanceType<Element> | Text | string | number)=
modified element
🟡 Property: i
J_CONFIG[]=
an array of subelements (items) of the created or modified element
🟡 Property: k
{
[key: string]: any;
}=
properties (keys) to set in the created or modified element
🟡 Property: n
string=
namespace for createElementNS, setAttributeNS and removeAttributeNS methods
🟡 Property: p
InstanceType<Element>=
reference to the parent element for the created or modified element
🟡 Property: t
string=
tag of the created element
🟥 Constant: NAMESPACE_SVG
const NAMESPACE_SVG: "http://www.w3.org/2000/svg";
🟨 Function: j
function j({ a, e, i, k, n, p, t }: J_CONFIG): E<InstanceType<Element> | Text>;
Lightweight helper for creating and modifying DOM elements.
🔴 Parameter: config
J_CONFIG
🟢 Returns
E<InstanceType<Element> | Text>
🟨 Function: j_style
function j_style(js_style: import('./jss.js').JSS, splitter?: string | undefined): E<HTMLStyleElement>;
Helper for creating <style> elements.
🔴 Parameter: js_style
import('./jss.js').JSS
🔴 Parameter: splitter
string=
🟢 Returns
E<HTMLStyleElement>
🟨 Function: j_svg
function j_svg(configs: J_CONFIG[]): E<SVGSVGElement>;
Helper for creating <svg> container elements.
🔴 Parameter: configs
J_CONFIG[]
🟢 Returns
E<SVGSVGElement>
🟨 Function: j_svg_use
function j_svg_use(id: string): J_CONFIG;
Helper for creating <svg><use> elements.
🔴 Parameter: id
string
🟢 Returns
J_CONFIG
🟨 Function: j_symbol
function j_symbol(config: J_CONFIG): E<SVGSymbolElement>;
Helper for creating <symbol> elements.
🔴 Parameter: config
J_CONFIG
🟢 Returns
E<SVGSymbolElement>
🟨 Function: toe
function toe<T>(e: T): E<T>;
🔵 Template parameter: T
🔴 Parameter: e
T
🟢 Returns
E<T>
📄 File: src/jcmp.js
Jackens’ Components.
Jackens’ Components configuration format
Jackens’ Components configuration format (JCMP_CONFIG) is based on three main keys:
w (wrapper): J_CONFIG format configuration of the component wrapper (HTMLDivElement element)
c (control): J_CONFIG format configuration of the component control
l (label): J_CONFIG format configuration of the component label (HTMLLabelElement element; applies to input controls of type checkbox, radio and file)
All other keys are just an alternative, more convenient form for configuration using the main keys.
The sample file selection button component shown below

has the following HTML representation:
<body>
<div class="jcmp" label="Label">
<input type="file" id="jcmp-1" />
<div>
<label for="jcmp-1">Text</label>
</div>
</div>
<script type="module">
import { j, j_style } from '../../src/j.js'
import { jcmp_jss } from '../../src/jcmp.js'
window.onload = () => j({
e: document.body,
i: [j_style(jcmp_jss())]
})
</script>
</body>
The file selection button component shown above can be generated with the following code:
<script type="module">
import { j, j_style } from '../../src/j.js'
import { jcmp, jcmp_jss } from '../../src/jcmp.js'
window.onload = () => j({
e: document.body,
i: [
j_style(jcmp_jss()),
jcmp({ type: 'file', label: 'Label', text: 'Text' })
]
})
</script>
The JCMP_CONFIG format has the following default values:
{ […], t: 'input', class: 'jcmp', […] }
The icon and text keys are handled in a special way: they allow you to conveniently specify the icon and text of the component you are defining.
Jackens’ Components layout system
Jackens’ Components layout system contains only two types of classes:
.w-«w»-«sw»: width of «w» slots when the screen is wide enough for «sw» slots.
.h-«h»-«sw»: height of «h» slots when the screen is wide enough for «sw» slots.
The minimum slot width is 200 pixels.
Example. Components defined as follows:
<script type="module">
import { j, j_style } from '../../src/j.js'
import { jcmp, jcmp_jss } from '../../src/jcmp.js'
window.onload = () => j({
e: document.body,
i: [
j_style(jcmp_jss()),
jcmp({
label: 'Component #1',
class: 'jcmp w-1-3 w-1-2',
text: 'jcmp w-1-3 w-1-2'
}), jcmp({
label: 'Component #2',
class: 'jcmp w-1-3 w-1-2',
text: 'jcmp w-1-3 w-1-2'
}), jcmp({
label: 'Component #3',
class: 'jcmp w-1-3',
text: 'jcmp w-1-3'
})
]
})
</script>
on a screen at least 600 pixels wide, will be arranged on a single line:

on a screen less than 600 pixels wide, but not less than 400 pixels wide, will be arranged in two lines:

while on a screen less than 400 pixels wide, they will be arranged in three rows:

🟩 Type: JCMP_CONFIG
type JCMP_CONFIG = {
w: import('./j.js').J_CONFIG | null;
c: import('./j.js').J_CONFIG | null;
l: import('./j.js').J_CONFIG | null;
class: string | null;
label: string | null;
p: HTMLElement | null;
onchange: any | null;
onclick: any | null;
oninput: any | null;
onkeyup: any | null;
style: {
[key: string]: any;
};
t: string | null;
icon: string | null;
text: string | null;
};
Jackens’ Components configuration.
🟡 Property: w
import('./j.js').J_CONFIG?
wrapper configuration
🟡 Property: c
import('./j.js').J_CONFIG?
control configuration
🟡 Property: l
import('./j.js').J_CONFIG?
label configuration
🟡 Property: class
string?
w.a.class configuration
🟡 Property: label
string?
w.a.label configuration
🟡 Property: p
HTMLElement?
w.p configuration
🟡 Property: onchange
any?
c.k.onchange configuration
🟡 Property: onclick
any?
c.k.onclick configuration
🟡 Property: oninput
any?
c.k.oninput configuration
🟡 Property: onkeyup
any?
c.k.onkeyup configuration
🟡 Property: style
{
[key: string]: any;
}?
c.k.style configuration
🟡 Property: t
string?
c.t configuration
🟡 Property: icon
string?
icon
🟡 Property: text
string?
text
🟨 Function: jcmp
function jcmp(config: JCMP_CONFIG): import('./j.js').J_CONFIG;
Convenient converter from JCMP_CONFIG format to J_CONFIG format.
import { it } from '../src/it.js'
import { j_svg_use } from '../src/j.js'
import { jcmp, jcmp_jss } from '../src/jcmp.js'
const svg_use_config = {
a: { fill: '#fff', stroke: '#fff', width: 17, height: 17 }
}
const button_1 = jcmp({
t: 'button',
w: {
a: { class: 'jcmp' }
},
c: {
a: { 'aria-label': 'button' },
i: [{
...j_svg_use('icon-id'), ...svg_use_config
}, {
t: 'div'
}, {
e: 'Button'
}],
k: {
style: { marginLeft: 42, marginRight: 17 },
onclick: console.log
}
}
})
const button_2 = jcmp({
t: 'button',
style: { marginLeft: 42, marginRight: 17 },
onclick: console.log,
text: 'Button',
icon: 'icon-id',
'aria-label': 'button'
})
it('should handle keys ‘style’, ‘onclick’ and ‘aria-label’', () => {
button_2.should.be.deep.equal(button_1)
})
const file_1 = jcmp({
t: 'input',
type: 'file',
w: {
a: { class: 'jcmp' }
},
l: {
i: [{ e: 'Choose file…' }]
}
})
const file_2 = jcmp({
t: 'input', type: 'file', text: 'Choose file…'
})
it('should add class ‘jcmp’ automatically and handle key ‘text’', () => {
file_2.i[0].a.id = file_2.i[1].i[0].a.for = 'jcmp-1'
file_2.should.be.deep.equal(file_1)
})
const css = jcmp_jss()
it('should generate proper CSS', () => {
css.should.be.string
})
🔴 Parameter: config
JCMP_CONFIG
🟢 Returns
import('./j.js').J_CONFIG
🟨 Function: jcmp_jss
function jcmp_jss({ font_family, focus_color, main_color }?: {
font_family?: string;
focus_color?: string;
main_color?: string;
}): import('./jss.js').JSS;
Jackens’ Components CSS rules in JSS format.
🔴 Parameter: config
{
font_family?: string;
focus_color?: string;
main_color?: string;
}
🟢 Returns
import('./jss.js').JSS
📄 File: src/js_on_parse.js
JSON.parse with “JavaScript turned on”.
Usage example from unit tests:
import { it } from '../src/it.js'
import { js_on_parse } from '../src/js_on_parse.js'
const handlers = {
join: (...params) => params.join(' '),
outer: text => `Hello ${text}!`,
inner: text => text.charAt(0).toUpperCase() + text.slice(1).toLowerCase(),
question: text => ({ d: 'Yes!', t: 42 }[text[0]])
}
it('should use ‘join’ handler', () => {
js_on_parse(`{
"join": ["Hello", "World!"]
}`, handlers).should.be.deep.equal('Hello World!')
})
it('should ‘join’ and ‘to_much’ handlers', () => {
js_on_parse(`{
"join": ["Hello", "World!"], "to_much": "whatever"
}`, handlers).should.be.deep.equal({
join: ['Hello', 'World!'], to_much: 'whatever'
})
})
it('should use ‘inner’ and ‘outer’ handlers', () => {
js_on_parse(`{
"outer":{ "inner": "wORld" }
}`, handlers).should.be.deep.equal('Hello World!')
})
it('should use ‘question’ handler', () => {
js_on_parse(`[{
"question": "does it really works?!?"
}, {
"question": "the answer to life the universe and everything"
}, {
"Question": null
}]`, handlers).should.be.deep.equal(['Yes!', 42, { Question: null }])
})
it('should process nested objects', () => {
js_on_parse('{"H":{"e":{"l":{"l":{"o":["World!"]}}}}}', handlers)
.should.be.deep.equal({ H: { e: { l: { l: { o: ['World!'] } } } } })
})
Objects having exactly one «handlerName» property present in the handlers map, i.e. objects of form:
{ "«handlerName»": «param» }
and
{ "«handlerName»": [«params»] }
are replaced by the result of call
handlers['«handlerName»'](«param»)
and
handlers['«handlerName»'](...«params»)
Remark. To pass to handlers['«handlerName»'] a single argument that is an array, use the following form:
{ "«handlerName»": [[«elements»]] }
which will force a call
handlers['«handlerName»']([«elements»])
🟨 Function: js_on_parse
function js_on_parse(text: string, handlers: {
[handler_name: string]: Function;
}): any;
JSON.parse with “JavaScript turned on”.
🔴 Parameter: text
string
🔴 Parameter: handlers
{
[handler_name: string]: Function;
}
🟢 Returns
any
📄 File: src/jss.js
CSS-in-JS helper based on the JSS format.
Usage example from unit tests:
import { it } from '../src/it.js'
import { jss } from '../src/jss.js'
it('should handle nested definitions (level 1)', () => {
const actual_1 = {
a: {
color: 'red',
margin: 1,
'.c': {
margin: 2,
padding: 2
},
padding: 1
}
}
const expected_1 =
'a{color:red;margin:1}' +
'a.c{margin:2;padding:2}' +
'a{padding:1}'
jss(actual_1).should.be.equal(expected_1)
})
it('should handle nested definitions (level 2)', () => {
const actual_2 = {
a: {
'.b': {
color: 'red',
margin: 1,
'.c': {
margin: 2,
padding: 2
},
padding: 1
}
}
}
const expected_2 =
'a.b{color:red;margin:1}' +
'a.b.c{margin:2;padding:2}' +
'a.b{padding:1}'
jss(actual_2).should.be.equal(expected_2)
})
it('should handle ‘@’ prefixes and ‘$$suffix’ suffixes', () => {
const actual_3 = {
'@font-face$$1': {
fontFamily: 'Jackens',
src$$1: 'url(fonts/jackens.otf)',
src$$2: "url(fonts/jackens.otf) format('opentype')," +
"url(fonts/jackens.svg) format('svg')",
fontWeight: 'normal',
fontStyle: 'normal'
},
'@font-face$$2': {
fontFamily: 'C64',
src: 'url(fonts/C64_Pro_Mono-STYLE.woff)'
},
'@keyframes spin': {
'0%': { transform: 'rotate(0deg)' },
'100%': { transform: 'rotate(360deg)' }
},
div: {
border: 'solid red 1px',
'.c1': { 'background-color': '#000' },
' .c1': { backgroundColor: 'black' },
'.c2': { backgroundColor: 'rgb(0,0,0)' }
},
'@media(min-width:200px)': {
div: { margin: 0, padding: 0 },
span: { color: '#000' }
}
}
const expected_3 = '@font-face{font-family:Jackens;src:url(fonts/jackens.otf);' +
"src:url(fonts/jackens.otf) format('opentype')," +
"url(fonts/jackens.svg) format('svg');" +
'font-weight:normal;font-style:normal}' +
'@font-face{font-family:C64;src:url(fonts/C64_Pro_Mono-STYLE.woff)}' +
'@keyframes spin{0%{transform:rotate(0deg)}100%{transform:rotate(360deg)}}' +
'div{border:solid red 1px}' +
'div.c1{background-color:#000}' +
'div .c1{background-color:black}' +
'div.c2{background-color:rgb(0,0,0)}' +
'@media(min-width:200px){div{margin:0;padding:0}span{color:#000}}'
jss(actual_3).should.be.equal(expected_3)
})
it('should handle comma separation (level 2)', () => {
const actual_4 = {
a: {
'.b,.c': {
margin: 1,
'.d': {
margin: 2
}
}
}
}
const expected_4 = 'a.b,a.c{margin:1}a.b.d,a.c.d{margin:2}'
jss(actual_4).should.be.equal(expected_4)
})
it('should handle comma separation (level 1)', () => {
const actual_5 = {
'.b,.c': {
margin: 1,
'.d': {
margin: 2
}
}
}
const expected_5 = '.b,.c{margin:1}.b.d,.c.d{margin:2}'
jss(actual_5).should.be.equal(expected_5)
})
it('should handle multiple comma separations', () => {
const actual_6 = {
'.a,.b': {
margin: 1,
'.c,.d': {
margin: 2
}
}
}
const expected_6 = '.a,.b{margin:1}' +
'.a.c,.a.d,.b.c,.b.d{margin:2}'
jss(actual_6).should.be.equal(expected_6)
})
🟩 Type: JSS
type JSS = {
[rule_or_attribute: string]: string | number | JSS;
};
The JSS format provides a hierarchical description of CSS rules.
- Keys of sub-objects whose values are NOT objects are treated as CSS attribute, and values are treated as values of those CSS attributes; the concatenation of keys of all parent objects is a CSS rule.
- All keys ignore the part starting with a splitter (default: '$$') sign until the end of the key (e.g.
src$$1 → src, @font-face$$1 → @font-face).
- In keys specifying CSS attribute, all uppercase letters are replaced by lowercase letters with an additional
- character preceding them (e.g. fontFamily → font-family).
- Commas in keys that makes a CSS rule cause it to “split” and create separate rules for each part (e.g.
{div:{margin:1,'.a,.b,.c':{margin:2}}} → div{margin:1}div.a,div.b,div.c{margin:2}).
- Top-level keys that begin with
@ are not concatenated with sub-object keys.
{
[rule_or_attribute: string]: string | number | JSS;
}
🟨 Function: jss
function jss(style: JSS, splitter?: string | undefined): string;
CSS-in-JS helper based on the JSS format.
An iterative implementation that does not cause a stack overflow exception.
🔴 Parameter: style
JSS
🔴 Parameter: splitter
string=
🟢 Returns
string
📄 File: src/mdoc.js
Converter from JSDoc to Markdown.
Usage example from unit tests (used to generate this documentation):
import { js_on_parse } from '../src/js_on_parse.js'
import { mdoc, read_file, write_file } from '../src/mdoc.js'
const config = {
write_file: {
lines: [
{ read_file: 'md/header.md' },
'\n# Sources\n',
{ mdoc: {} },
'\n# License\n',
{ read_file: 'LICENSE' },
'\n',
{ read_file: 'md/footer.md' }
],
path: 'readme.md'
}
}
js_on_parse(JSON.stringify(config), { mdoc, read_file, write_file })
🟩 Type: MDOC_BLOCK
type MDOC_BLOCK = {
_raw_chunk?: string;
chunks: MDOC_CHUNK[];
d_ts?: string;
ignore?: boolean;
kind?: string;
};
{
_raw_chunk?: string;
chunks: MDOC_CHUNK[];
d_ts?: string;
ignore?: boolean;
kind?: string;
}
🟩 Type: MDOC_CHUNK
type MDOC_CHUNK = {
name?: string;
tag?: string;
texts: string[];
type_spec?: string;
};
{
name?: string;
tag?: string;
texts: string[];
type_spec?: string;
}
🟩 Type: MDOC_FILE
type MDOC_FILE = {
blocks: {
[block_name: string]: MDOC_BLOCK;
};
main_block?: MDOC_BLOCK;
};
{
blocks: {
[block_name: string]: MDOC_BLOCK;
};
main_block?: MDOC_BLOCK;
}
🟨 Function: mdoc
function mdoc({ src, extra_ids, top_level_header, debug_json }?: {
debug_json?: string;
extra_ids?: boolean;
src?: string[];
top_level_header?: 1 | 2 | 3 | 4;
}): string;
Converter from JSDoc to Markdown.
🔴 Parameter: config
{
debug_json?: string;
extra_ids?: boolean;
src?: string[];
top_level_header?: 1 | 2 | 3 | 4;
}
debug_json: optional path to write debug JSON file
extra_ids: flag to insert extra ids (<a id="…" name="…"></a>) — useful in case of some Markdown renderers (i.e. BitBucket)
src: directories to scan
top_level_header: top level HTML header
🟢 Returns
string
🟨 Function: read_file
function read_file(path: string): string;
Handler that reads the contents of the specified file.
🔴 Parameter: path
string
🟢 Returns
string
🟨 Function: write_file
function write_file({ lines, path }: {
lines: string[];
path: string;
}): string;
Helper that writes an array of lines to the specified file.
🔴 Parameter: config
{
lines: string[];
path: string;
}
🟢 Returns
string
📄 File: src/num_to_key.js
Helper for converting integers to map-friendly string identifiers.
Usage example from unit tests:
import { it } from '../src/it.js'
import { num_to_key } from '../src/num_to_key.js'
const ALPHA = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$'
const _1_TO_9 = '123456789'
const _0_TO_9 = `0${_1_TO_9}`
const ALPHA_NUM = `${_0_TO_9}${ALPHA}`
const KEYS = []
for (let a = 0; a < ALPHA_NUM.length; ++a) {
KEYS.push(ALPHA_NUM[a])
}
for (let a = 0; a < _1_TO_9.length; ++a) {
for (let b = 0; b < _0_TO_9.length; ++b) {
KEYS.push(`${_1_TO_9[a]}${_0_TO_9[b]}`)
}
}
for (let a = 0; a < ALPHA.length; ++a) {
for (let b = 0; b < ALPHA_NUM.length; ++b) {
KEYS.push(`${ALPHA[a]}${ALPHA_NUM[b]}`)
}
}
for (let a = 0; a < _1_TO_9.length; ++a) {
for (let b = 0; b < _0_TO_9.length; ++b) {
for (let c = 0; c < _0_TO_9.length; ++c) {
KEYS.push(`${_1_TO_9[a]}${_0_TO_9[b]}${_0_TO_9[c]}`)
}
}
}
for (let a = 0; a < ALPHA.length; ++a) {
for (let b = 0; b < ALPHA_NUM.length; ++b) {
for (let c = 0; c < ALPHA_NUM.length; ++c) {
KEYS.push(`${ALPHA[a]}${ALPHA_NUM[b]}${ALPHA_NUM[c]}`)
}
}
}
it('should work', () => {
for (let num = 0; num < KEYS.length; ++num) {
num_to_key(num).should.be.equal(KEYS[num])
}
})
🟨 Function: num_to_key
function num_to_key(num: number): string;
Helper for converting integers to map-friendly string identifiers.
🔴 Parameter: num
number
🟢 Returns
string
📄 File: src/pl_ural.js
Helper for choosing the correct singular and plural.
Usage example from unit tests:
import { it } from '../src/it.js'
import { pl_ural } from '../src/pl_ural.js'
it('should handle 0', () => {
pl_ural(0, 'car', 'cars', 'cars').should.be.equal('0 cars')
pl_ural(0, 'auto', 'auta', 'aut').should.be.equal('0 aut')
})
it('should handle 1', () => {
pl_ural(1, 'car', 'cars', 'cars').should.be.equal('1 car')
pl_ural(1, 'auto', 'auta', 'aut').should.be.equal('1 auto')
})
it('should handle 5', () => {
pl_ural(5, 'car', 'cars', 'cars').should.be.equal('5 cars')
pl_ural(5, 'auto', 'auta', 'aut').should.be.equal('5 aut')
})
it('should handle 2', () => {
pl_ural(42, 'car', 'cars', 'cars').should.be.equal('42 cars')
pl_ural(42, 'auto', 'auta', 'aut').should.be.equal('42 auta')
})
🟨 Function: pl_ural
function pl_ural(value: number, singular: string, plural_2: string, plural_5: string, no_value_1?: string | undefined, no_value?: string | undefined): string;
Helper for choosing the correct singular and plural.
🔴 Parameter: value
number
🔴 Parameter: singular
string
🔴 Parameter: plural_2
string
🔴 Parameter: plural_5
string
🔴 Parameter: no_value_1
string=
🔴 Parameter: no_value
string=
🟢 Returns
string
📄 File: src/preloader.js
Animated gear preloader.
🟥 Constant: preloader
const preloader: import('./j.js').E<SVGSVGElement>;
Animated gear preloader.
🟠 Type
import('./j.js').E<SVGSVGElement>
🟥 Constant: preloader_jss
const preloader_jss: import('./jss.js').JSS;
Animated gear preloader CSS rules in JSS format.
🟠 Type
import('./jss.js').JSS
📄 File: src/scan_dirs.js
Helper to scan files in the specified directories.
Unit tests:
import { it } from '../src/it.js'
import { scan_dirs } from '../src/scan_dirs.js'
it('should work', async () => {
const src = scan_dirs('src')
const test = scan_dirs('test')
const src_and_test = scan_dirs('src', 'test')
src.length.should.be.greaterThan(20)
test.length.should.be.greaterThan(30)
src_and_test.length.should.be.equal(src.length + test.length)
})
🟨 Function: scan_dirs
function scan_dirs(...dirs: string[]): string[];
Helper to scan files in the specified directories.
🔴 Parameter: dirs
...string
🟢 Returns
string[]
📄 File: src/sql.js
A set of helpers for creating safe SQL queries.
Usage example from unit tests:
import { it } from '../src/it.js'
import { mssql_name, mysql_name, sql, sql_name } from '../src/sql.js'
it('should work', () => {
const actual = sql`
SELECT *
FROM ${mysql_name('table`name')}
WHERE
${sql_name('column"name')} = ${42} OR
${mssql_name('[column][name]')} = ${"'"} OR
column_name = ${true} OR
column_name = ${false} OR
column_name = ${new Date('1980-03-31T04:30:00.000Z')} OR
column_name IN (${[42, "'", true, false, new Date('1980-03-31T04:30:00.000Z')]})`
const expected = `
SELECT *
FROM \`table\`\`name\`
WHERE
"column""name" = 42 OR
[[column]][name]]] = '''' OR
column_name = b'1' OR
column_name = b'0' OR
column_name = '1980-03-31 04:30:00' OR
column_name IN (42,'''',b'1',b'0','1980-03-31 04:30:00')`
actual.should.be.equal(expected)
})
🟩 Type: SQL_NAME
type SQL_NAME = {
[Symbol.toStringTag]: '__sql_name';
toString: () => string;
};
A special type for functions escaping SQL column, table and schema names.
{
[Symbol.toStringTag]: '__sql_name';
toString: () => string;
}
🟨 Function: mssql_name
function mssql_name(name: string): SQL_NAME;
Helper for escaping MS SQL column, table and schema names.
🔴 Parameter: name
string
🟢 Returns
SQL_NAME
🟨 Function: mysql_name
function mysql_name(name: string): SQL_NAME;
Helper for escaping MySQL/MariaDB column, table and schema names.
🔴 Parameter: name
string
🟢 Returns
SQL_NAME
🟨 Function: sql
function sql(template: TemplateStringsArray, ...substitutions: any[]): string;
Tagged Template Literal helper for creating secure SQL queries.
🔴 Parameter: template
TemplateStringsArray
🔴 Parameter: substitutions
any[]
🟢 Returns
string
🟨 Function: sql_name
function sql_name(name: string): SQL_NAME;
Helper for escaping SQL column, table and schema names.
🔴 Parameter: name
string
🟢 Returns
SQL_NAME
🟥 Constant: sql_type
const sql_type: {
[type: string]: (value: any) => string;
};
Methods that implement value escape for particular types.
🟠 Type
{
[type: string]: (value: any) => string;
}
🟨 Function: sql_value
function sql_value(value: any): string;
Helper for escaping values.
🔴 Parameter: value
any
🟢 Returns
string
📄 File: src/stream_to_buffer.js
Helper for converting streams to buffers.
Usage example from unit tests:
import { createReadStream } from 'fs'
import { it } from '../src/it.js'
import { stream_to_buffer } from '../src/stream_to_buffer.js'
it('should work', async () => {
const stream = createReadStream('src/stream_to_buffer.js')
const buffer = await stream_to_buffer(stream)
buffer.toString('utf8').includes('export const stream_to_buffer =')
.should.be.true
})
🟨 Function: stream_to_buffer
function stream_to_buffer(stream: import('stream').Stream): Promise<Buffer>;
Helper for converting streams to buffers.
🔴 Parameter: stream
import('stream').Stream
🟢 Returns
Promise<Buffer>
📄 File: src/translate.js
Language translations helper.
Usage example from unit tests:
import { it } from '../src/it.js'
import { translate } from '../src/translate.js'
const locales = {
pl: {
Password: 'Hasło',
button: { Login: 'Zaloguj' }
}
}
const _ = translate.bind(0, locales, 'pl')
it('should handle defined texts', () => {
_('Login').should.be.equal('Login')
_('Password').should.be.equal('Hasło')
})
it('should handle undefined text', () => {
_('Undefined text').should.be.equal('Undefined text')
})
it('should handle defined version', () => {
_('Login', 'button').should.be.equal('Zaloguj')
})
it('should handle undefined version', () => {
_('Password', 'undefined_version').should.be.equal('Hasło')
_('Undefined text', 'undefined_version').should.be.equal('Undefined text')
})
it('should have empty prototype', () => {
_('toString').should.be.equal('toString')
_('toString', 'undefined_version').should.be.equal('toString')
})
🟨 Function: get_navigator_language
function get_navigator_language(locales: {
[lang: string]: {
[text_or_version: string]: string | {
[text: string]: string;
};
};
}, default_language: string): string;
🔴 Parameter: locales
{
[lang: string]: {
[text_or_version: string]: string | {
[text: string]: string;
};
};
}
🔴 Parameter: default_language
string
🟨 Function: translate
function translate(locales: {
[lang: string]: {
[text_or_version: string]: string | {
[text: string]: string;
};
};
}, language: string, text: string, version?: string | undefined): string;
Language translations helper.
🔴 Parameter: locales
{
[lang: string]: {
[text_or_version: string]: string | {
[text: string]: string;
};
};
}
🔴 Parameter: language
string
🔴 Parameter: text
string
🔴 Parameter: version
string=
🟢 Returns
string
📄 File: src/type_of.js
A collection of type testing helpers.
Usage example from unit tests:
import { it } from '../src/it.js'
import {
is_Array,
is_AsyncFunction,
is_Boolean,
is_Date,
is_Element,
is_Function,
is_GeneratorFunction,
is_HTMLButtonElement,
is_HTMLInputElement,
is_HTMLTextAreaElement,
is_Number,
is_Object,
is_Promise,
is_RegExp,
is_String,
is_Text,
type_of
} from '../src/type_of.js'
it('should work', () => {
expect(typeof []).to.be.equal('object')
type_of([]).should.be.equal('Array')
is_Array([]).should.be.true
expect(typeof (async () => { })).to.be.equal('function')
type_of(async () => { }).should.be.equal('AsyncFunction')
is_AsyncFunction(async () => { }).should.be.true
expect(typeof true).to.be.equal('boolean')
type_of(true).should.be.equal('Boolean')
is_Boolean(true).should.be.true
expect(typeof new Date()).to.be.equal('object')
type_of(new Date()).should.be.equal('Date')
is_Date(new Date()).should.be.true
expect(typeof document.createElement('DIV')).to.be.equal('object')
type_of(document.createElement('DIV')).should.be.equal('HTMLDivElement')
is_Element(document.createElement('DIV')).should.be.true
expect(typeof function * () {}).to.be.equal('function')
type_of(function * () {}).should.be.equal('GeneratorFunction')
is_GeneratorFunction(function * () {}).should.be.true
expect(typeof type_of).to.be.equal('function')
type_of(type_of).should.be.equal('Function')
is_Function(type_of).should.be.true
is_HTMLButtonElement(document.createElement('BUTTON')).should.be.true
is_HTMLInputElement(document.createElement('INPUT')).should.be.true
is_HTMLTextAreaElement(document.createElement('TEXTAREA')).should.be.true
expect(typeof 42).to.be.equal('number')
type_of(42).should.be.equal('Number')
is_Number(42).should.be.true
expect(typeof NaN).to.be.equal('number')
type_of(NaN).should.be.equal('Number')
is_Number(NaN).should.be.true
expect(typeof Infinity).to.be.equal('number')
type_of(Infinity).should.be.equal('Number')
is_Number(Infinity).should.be.true
expect(typeof {}).to.be.equal('object')
type_of({}).should.be.equal('Object')
is_Object({}).should.be.true
expect(typeof Promise.resolve()).to.be.equal('object')
type_of(Promise.resolve()).should.be.equal('Promise')
is_Promise(Promise.resolve()).should.be.true
expect(typeof /^(Reg)(Exp)$/).to.be.equal('object')
type_of(/^(Reg)(Exp)$/).should.be.equal('RegExp')
is_RegExp(/^(Reg)(Exp)$/).should.be.true
expect(typeof 'Jackens').to.be.equal('string')
type_of('Jackens').should.be.equal('String')
is_String('Jackens').should.be.true
expect(typeof String('Jackens')).to.be.equal('string')
type_of(String('Jackens')).should.be.equal('String')
is_String(String('Jackens')).should.be.true
expect(typeof new String('Jackens')).to.be.equal('object')
type_of(new String('Jackens')).should.be.equal('String')
is_String(new String('Jackens')).should.be.true
is_Text(document.createTextNode('')).should.be.true
})
🟨 Function: is_Array
function is_Array(arg: any): arg is any[];
Helper that checks if the arg is of type Array.
🔴 Parameter: arg
any
🟢 Returns
arg is Array
🟨 Function: is_AsyncFunction
function is_AsyncFunction(arg: any): boolean;
Helper that checks if the arg is of type AsyncFunction.
🔴 Parameter: arg
any
🟢 Returns
boolean
🟨 Function: is_Boolean
function is_Boolean(arg: any): arg is boolean;
Helper that checks if the arg is of type Boolean.
🔴 Parameter: arg
any
🟢 Returns
arg is Boolean
🟨 Function: is_Date
function is_Date(arg: any): arg is Date;
Helper that checks if the arg is of type Date.
🔴 Parameter: arg
any
🟢 Returns
arg is Date
🟨 Function: is_Element
function is_Element(arg: any): arg is Element;
Helper that checks if the arg is of type Element.
🔴 Parameter: arg
any
🟢 Returns
arg is Element
🟨 Function: is_Function
function is_Function(arg: any): arg is Function;
Helper that checks if the arg is of type Function.
🔴 Parameter: arg
any
🟢 Returns
arg is Function
🟨 Function: is_GeneratorFunction
function is_GeneratorFunction(arg: any): arg is GeneratorFunction;
Helper that checks if the arg is of type GeneratorFunction.
🔴 Parameter: arg
any
🟢 Returns
arg is GeneratorFunction
🟨 Function: is_HTMLButtonElement
function is_HTMLButtonElement(arg: any): arg is HTMLButtonElement;
Helper that checks if the arg is of type HTMLButtonElement.
🔴 Parameter: arg
any
🟢 Returns
arg is HTMLButtonElement
🟨 Function: is_HTMLInputElement
function is_HTMLInputElement(arg: any): arg is HTMLInputElement;
Helper that checks if the arg is of type HTMLInputElement.
🔴 Parameter: arg
any
🟢 Returns
arg is HTMLInputElement
🟨 Function: is_HTMLTextAreaElement
function is_HTMLTextAreaElement(arg: any): arg is HTMLTextAreaElement;
Helper that checks if the arg is of type HTMLTextAreaElement.
🔴 Parameter: arg
any
🟢 Returns
arg is HTMLTextAreaElement
🟨 Function: is_Number
function is_Number(arg: any): arg is number;
Helper that checks if the arg is of type Number.
🔴 Parameter: arg
any
🟢 Returns
arg is Number
🟨 Function: is_Object
function is_Object(arg: any): arg is any;
Helper that checks if the arg is of type Object.
🔴 Parameter: arg
any
🟢 Returns
arg is Object
🟨 Function: is_Promise
function is_Promise(arg: any): arg is Promise<any>;
Helper that checks if the arg is of type Promise.
🔴 Parameter: arg
any
🟢 Returns
arg is Promise
🟨 Function: is_RegExp
function is_RegExp(arg: any): arg is RegExp;
Helper that checks if the arg is of type RegExp.
🔴 Parameter: arg
any
🟢 Returns
arg is RegExp
🟨 Function: is_String
function is_String(arg: any): arg is string;
Helper that checks if the arg is of type String.
🔴 Parameter: arg
any
🟢 Returns
arg is String
🟨 Function: is_Text
function is_Text(arg: any): arg is Text;
Helper that checks if the arg is of type Text.
🔴 Parameter: arg
any
🟢 Returns
arg is Text
🟨 Function: type_of
function type_of(arg: any): string;
Replacement for the typeof operator that works properly.
🔴 Parameter: arg
any
🟢 Returns
string
📄 File: src/uuid_v1.js
A collection of UUID v1 helpers.
Usage example from unit tests:
import { it } from '../src/it.js'
import { uuid_v1, uuid_v1_decode } from '../src/uuid_v1.js'
it('should contain a global counter', () => {
for (let i = 0; i < 4200; ++i) {
const counter = uuid_v1().split('-')[3]
if (i === 0) {
counter.should.be.equal('8001')
} else if (i === 4094) {
counter.should.be.equal('8fff')
} else if (i === 4095) {
counter.should.be.equal('8000')
} else if (i === 4096) {
counter.should.be.equal('8001')
}
}
})
it('should match regexp pattern', () => {
uuid_v1().should.match(
/^[\da-f]{8}-[\da-f]{4}-1[\da-f]{3}-8[\da-f]{3}-[\da-f]{12}$/)
})
it('should support the specified date and value', () => {
const uuid_1 = uuid_v1(new Date(323325e6), 205163983024656)
uuid_1.startsWith('c1399400-9a71-11bd').should.be.true
uuid_1.endsWith('-ba9876543210').should.be.true
const uuid_2 = uuid_v1(new Date(323325e6), 486638959735312)
uuid_2.startsWith('c1399400-9a71-11bd').should.be.true
uuid_2.endsWith('-ba9876543210').should.be.true
})
it('should decode date', () => {
const date_1 = new Date()
const uuid = uuid_v1(date_1)
const date_2 = uuid_v1_decode(uuid)
expect(+date_2).to.be.equal(+date_1)
})
🟨 Function: uuid_v1
function uuid_v1(date?: Date | undefined, value?: string | undefined): string;
UUID v1 identifier (containing creation timestamp) generator.
🔴 Parameter: date
Date=
🔴 Parameter: value
string=
🟢 Returns
string
🟨 Function: uuid_v1_decode
function uuid_v1_decode(uuid: string): Date;
Date extractor from UUID v1 identifier.
🔴 Parameter: uuid
string
🟢 Returns
Date
📄 File: src/wsc.js
Generic JSON RPC 2.0 WebSocket Client.
Usage examples:
<script type="module">
import { wsc } from '../../src/wsc.js'
window.onload = async () => {
const fs = await wsc('ws://localhost:12345/')
const list = await fs('list', { path: 'src' })
const file = await fs('read', { path: 'src/wsc.js' })
console.log(list)
console.log(file)
}
</script>
<script type="module">
import { wsc } from '../../src/wsc.js'
window.onload = async () => {
const mariadb = await wsc('ws://localhost:13306/')
const status = await mariadb('connect', {
config: { host: 'localhost', user: 'root', password: 'qwerty' }
})
const rows = await mariadb('query', { query: 'SELECT CURRENT_TIMESTAMP' })
console.log(status)
console.log(rows)
}
</script>
🟩 Type: WSC_HANDLERS
type WSC_HANDLERS = WSC_HANDLERS;
{
[id: string]: [Function, Function];
}
🟩 Type: WSC_SEND
type WSC_SEND = (method: string, params: {
[key: string]: any;
}) => Promise<any>;
🔴 Parameter: method
string
🔴 Parameter: params
{
[key: string]: any;
}
🟢 Returns
Promise<any>
🟨 Function: wsc
function wsc(url: string, handlers?: WSC_HANDLERS | undefined): Promise<WSC_SEND>;
Generic JSON RPC 2.0 WebSocket Client.
🔴 Parameter: url
string
🔴 Parameter: handlers
WSC_HANDLERS=
🟢 Returns
Promise<WSC_SEND>
📄 File: src/wss.js
Generic JSON RPC 2.0 WebSocket Server.
🟩 Type: WSS_METHOD
type WSS_METHOD = (param0: {
clients: WebSocket[];
client: WebSocket;
params: {
[param_name: string]: any;
};
}) => Promise<any>;
🔴 Parameter: param0
{
clients: WebSocket[];
client: WebSocket;
params: {
[param_name: string]: any;
};
}
🟢 Returns
Promise<any>
🟨 Function: wss
function wss(methods: {
[method_name: string]: WSS_METHOD;
}, port: number): void;
Generic JSON RPC 2.0 WebSocket Server.
🔴 Parameter: methods
{
[method_name: string]: WSS_METHOD;
}
🔴 Parameter: port
number
📄 File: src/wss_fs.js
File System JSON RPC 2.0 WebSocket Server.
Usage example:
import { wss_fs } from '../../src/wss_fs.js'
wss_fs(12345)
🟥 Constant: wss_fs
const wss_fs: (port: number) => void;
File System JSON RPC 2.0 WebSocket Server.
🟠 Type
(port: number) => void
📄 File: src/wss_mariadb.js
MariaDB JSON RPC 2.0 WebSocket Server.
Usage example:
import { wss_mariadb } from '../../src/wss_mariadb.js'
wss_mariadb(13306)
🟥 Constant: wss_mariadb
const wss_mariadb: (port: number) => void;
MariaDB JSON RPC 2.0 WebSocket Server.
🟠 Type
(port: number) => void
License
MIT License
Copyright (c) 2016+ Jackens
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Generated with MDoc.