stdtraits

Highly gen­eric traits/​in­ter­faces.

Laws

These ap­ply to all traits in this mod­ule.

  • Ar­rays, Strings and any list-like data struc­tures are treated as map­pings from in­dex -> value
  • Ar­ray sparse­ness is ig­nored on read; any list-like con­tainer has a spe­cific in­dex if size(my­Con­tainer) => key; get­ting an un­set value yields un­defined
  • Sets are treated as map­pings from a key to it­self
Source:

Interfaces

Immutable
Equals
Size
Shallowclone
Deepclone
Pairs
Get
Has
Assign
Delete
Setdefault
Replace

Members

(inner, constant) get

Source:

Given a key, get a value from a con­tainer.

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.

(inner, constant) has

Source:

Test if a con­tainer in­cludes an entry with the given key

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.

(inner, constant) assign

Source:

Set a value in a con­tainer. Al­ways re­turns the given value.

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.

(inner, constant) del

Source:

De­lete an entry with the given key from a con­tainer

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.

(inner, constant) setdefault

Source:

Set a de­fault value in a con­tainer.

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.

(inner, constant) replace

Source:

Swap out one value in a con­tainer for an­other

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.

Methods

(inner) typeIsImmutable(t) → {Boolean}

Source:
See:

Test whether in­stance of a given type is im­mut­able.

typeIs­Im­mut­able(Ob­ject); // => false
typeIs­Im­mut­able(String); // => true
Parameters:
Name Type Description
t Type
Returns:
Type
Boolean

(inner) isImmutable(v) → {Boolean}

Source:
See:

Test whether a given value is im­mut­able.

isIm­mut­able({}); // => false
isIm­mut­able(42); // => true
Parameters:
Name Type Description
v T
Returns:
Type
Boolean

(inner) eq(a, b) → {Boolean}

Source:
See:

De­term­ine whether two val­ues are equal us­ing the Equals trait.

eq([{foo: 42}], [{foo: 42}]); // => true
eq(1, 2); // => false

This func­tion is a bit more power­ful than than the Equals trait it­self: First of all it searches for a Equals im­ple­ment­a­tion for both ar­gu­ments and it falls back to === if none is found. For this reason us­ing eq() is usu­ally pre­ferred over us­ing the Equals trait dir­ectly.

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.
Parameters:
Name Type Description
a A
b B
Returns:
Type
Boolean

(inner) uneq(a, b) → {Boolean}

Source:
See:

Equi­val­ent to !eq(a, b)

uneq(4, 5); # => true

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.
Parameters:
Name Type Description
a A
b B
Returns:
Type
Boolean

(inner) assertEquals(a, b, msg)

Source:
See:

As­sert that eq(ac­tual, ex­pec­ted)

as­ser­tEquals([{foo: 42}], [{foo: 42}]); // OK!
as­ser­tEquals(1, 2); // As­ser­tion­Error!

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.
Parameters:
Name Type Description
a A
b B
msg String | undefined

The er­ror mes­sage to print

Throws:
AssertionError

(inner) assertUneq(a, b, msg)

Source:
See:

As­sert that !eq(ac­tual, ex­pec­ted)

as­ser­tUneq(1, 2); // OK!
as­ser­tUneq([{foo: 42}], [{foo: 42}]); // As­ser­tion­Error!

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.
Parameters:
Name Type Description
a A
b B
msg String | undefined

The er­ror mes­sage to print

Throws:

AssertionError

(inner) size(what) → {Number}

Source:
See:

De­term­ine the size of a con­tainer. Uses the Size trait.

size({foo: 42}); // => 1
size([1,2,3]); // => 3

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.
Parameters:
Name Type Description
what T
Returns:
Type
Number

(inner) empty(what) → {Boolean}

Source:
See:

De­term­ine if a con­tainer is empty. Uses size(x) === 0a

empty([]); // => true
empty({}); // => true
empty("asd"); // => false

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.
Parameters:
Name Type Description
what T
Returns:
Type
Boolean

(inner) shallowclone(a) → {A}

Source:
See:

Shal­lowly clone an ob­ject

const a = {foo: []};
const b = shal­low­clone(a);
b.foo.push(42);

a;  // => {foo: [42]}
b;  // => {foo: [42]}

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.
Parameters:
Name Type Description
a A
Returns:
Type
A

(inner) deepclone(a) → {A}

Source:
See:

Re­curs­ively clone an ob­ject

const a = {foo: []};
const b = deep­clone(a);
b.foo.push(42);

a;  // => {foo: []}
b;  // => {foo: [42]}

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.
Parameters:
Name Type Description
a A
Returns:
Type
A

(inner) pairs(what) → {Array}

Source:
See:

Get an iter­ator over any con­tainer. Al­ways re­turns pairs [key, value], this dis­tin­guishes pairs() from iter()/nor­mal it­er­a­tion.

Note that usu­ally you should use iter() over pairs un­less you really know that for­cing a con­tainer into key/​value rep­res­ent­a­tion is needed (e.g. Ar­ray with in­dices) since pairs() will only work on a very se­lect num­ber of con­tain­ers, while iter() will work on any iter­at­ors and will ac­tu­ally sup­port lists of key value pairs.

const {list, pairs} = re­quire('fer­rum');

list(pairs(['a', 'b'])); // => [[0, 'a'], [1, 'b']]
list(pairs(new Set[1, 2])); // => [[1, 1], [2, 2]]
list(pairs({foo: 42})); // [['foo', 42]]

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.
Parameters:
Name Type Description
what T
Yields:
Key/Value Pairs
Type
Array

(generator, inner) keys(what)

Source:
See:

Get an iter­ator over the keys of a con­tainer. Uses pairs(c).

const {list, keys} = re­quire('fer­rum');

list(keys(['a', 'b'])); // => [0, 1]
list(keys(new Set[1, 2])); // => [1, 2]
list(keys({foo: 42})); // ['foo']

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.
Parameters:
Name Type Description
what T
Yields:
The keys of the container

(generator, inner) values(what)

Source:
See:

Get an iter­ator over the val­ues of a con­tainer. Uses pairs(c).

const {list, val­ues} = re­quire('fer­rum');

list(val­ues(['a', 'b'])); // => ['a', 'b']
list(val­ues(new Set[1, 2])); // => [1, 2]
list(val­ues({foo: 42})); // [42]

Ver­sion his­tory

  • 1.2.0 Sup­port for ob­jects with Sym­bol keys.
Parameters:
Name Type Description
what T
Yields:
The values of the container