Highly generic traits/interfaces.
Laws
These apply to all traits in this module.
- Arrays, Strings and any list-like data structures are treated as mappings from index -> value
- Array sparseness is ignored on read; any list-like container
hasa specific index ifsize(myContainer) => key; getting an unset value yieldsundefined - Sets are treated as mappings from a key to itself
- Source:
Interfaces
Members
(inner, constant) get
- Source:
Given a key, get a value from a container.
(inner, constant) has
- Source:
Test if a container includes an entry with the given key
(inner, constant) assign
- Source:
Set a value in a container. Always returns the given value.
(inner, constant) del
- Source:
Delete an entry with the given key from a container
(inner, constant) setdefault
- Source:
Set a default value in a container.
(inner, constant) replace
- Source:
Swap out one value in a container for another
Methods
(inner) typeIsImmutable(t) → {Boolean}
- Source:
- See:
Test whether instance of a given type is immutable.
typeIsImmutable(Object); // => false
typeIsImmutable(String); // => true
Parameters:
| Name | Type | Description |
|---|---|---|
t |
Type |
Returns:
- Type
- Boolean
(inner) isImmutable(v) → {Boolean}
- Source:
- See:
-
Immutable for examples
Test whether a given value is immutable.
isImmutable({}); // => false
isImmutable(42); // => true
Parameters:
| Name | Type | Description |
|---|---|---|
v |
T |
Returns:
- Type
- Boolean
(inner) eq(a, b) → {Boolean}
- Source:
- See:
Determine whether two values are equal using the Equals trait.
eq([{foo: 42}], [{foo: 42}]); // => true
eq(1, 2); // => false
This function is a bit more powerful than than the Equals trait itself:
First of all it searches for a Equals implementation for both arguments
and it falls back to === if none is found.
For this reason using eq() is usually preferred over using the Equals trait directly.
Parameters:
| Name | Type | Description |
|---|---|---|
a |
A | |
b |
B |
Returns:
- Type
- Boolean
(inner) uneq(a, b) → {Boolean}
- Source:
- See:
-
Equals for examples
Equivalent to !eq(a, b)
uneq(4, 5); # => true
Parameters:
| Name | Type | Description |
|---|---|---|
a |
A | |
b |
B |
Returns:
- Type
- Boolean
(inner) assertEquals(a, b, msg)
- Source:
- See:
-
Equals for examples
Assert that eq(actual, expected)
assertEquals([{foo: 42}], [{foo: 42}]); // OK!
assertEquals(1, 2); // AssertionError!
Parameters:
| Name | Type | Description |
|---|---|---|
a |
A | |
b |
B | |
msg |
String | undefined | The error message to print |
Throws:
(inner) assertUneq(a, b, msg)
- Source:
- See:
-
Equals for examples
Assert that !eq(actual, expected)
assertUneq(1, 2); // OK!
assertUneq([{foo: 42}], [{foo: 42}]); // AssertionError!
Parameters:
| Name | Type | Description |
|---|---|---|
a |
A | |
b |
B | |
msg |
String | undefined | The error message to print |
Throws:
AssertionError
(inner) size(what) → {Number}
- Source:
- See:
-
Size for examples
Determine the size of a container. Uses the Size trait.
size({foo: 42}); // => 1
size([1,2,3]); // => 3
Parameters:
| Name | Type | Description |
|---|---|---|
what |
T |
Returns:
- Type
- Number
(inner) empty(what) → {Boolean}
- Source:
- See:
-
Size for examples
Determine if a container is empty. Uses size(x) === 0a
empty([]); // => true
empty({}); // => true
empty("asd"); // => false
Parameters:
| Name | Type | Description |
|---|---|---|
what |
T |
Returns:
- Type
- Boolean
(inner) shallowclone(a) → {A}
- Source:
- See:
-
Shallowclone for examples
Shallowly clone an object
const a = {foo: []};
const b = shallowclone(a);
b.foo.push(42);
a; // => {foo: [42]}
b; // => {foo: [42]}
Parameters:
| Name | Type | Description |
|---|---|---|
a |
A |
Returns:
- Type
- A
(inner) deepclone(a) → {A}
- Source:
- See:
-
Deepclone for examples
Recursively clone an object
const a = {foo: []};
const b = deepclone(a);
b.foo.push(42);
a; // => {foo: []}
b; // => {foo: [42]}
Parameters:
| Name | Type | Description |
|---|---|---|
a |
A |
Returns:
- Type
- A
(inner) pairs(what) → {Array}
- Source:
- See:
Get an iterator over any container.
Always returns pairs [key, value], this distinguishes pairs()
from iter()/normal iteration.
Note that usually you should use iter() over pairs unless you
really know that forcing a container into key/value representation
is needed (e.g. Array with indices) since pairs() will only work
on a very select number of containers, while iter() will work on
any iterators and will actually support lists of key value pairs.
const {list, pairs} = require('ferrum');
list(pairs(['a', 'b'])); // => [[0, 'a'], [1, 'b']]
list(pairs(new Set[1, 2])); // => [[1, 1], [2, 2]]
list(pairs({foo: 42})); // [['foo', 42]]
Parameters:
| Name | Type | Description |
|---|---|---|
what |
T |
Yields:
- Type
- Array
(generator, inner) keys(what)
- Source:
- See:
Get an iterator over the keys of a container. Uses pairs(c).
const {list, keys} = require('ferrum');
list(keys(['a', 'b'])); // => [0, 1]
list(keys(new Set[1, 2])); // => [1, 2]
list(keys({foo: 42})); // ['foo']
Parameters:
| Name | Type | Description |
|---|---|---|
what |
T |
Yields:
(generator, inner) values(what)
- Source:
- See:
Get an iterator over the values of a container. Uses pairs(c).
const {list, values} = require('ferrum');
list(values(['a', 'b'])); // => ['a', 'b']
list(values(new Set[1, 2])); // => [1, 2]
list(values({foo: 42})); // [42]
Parameters:
| Name | Type | Description |
|---|---|---|
what |
T |