new Collection(values)
Create a new collection.
Name | Type | Argument | Description |
---|---|---|---|
values |
object
|
optional | Values to add in the collection. |
Type | Description |
---|---|
TypeError
|
If the arguments |
var items = new Collection();
Properties
-
_container :object
-
Items container. All values of the collection are stored in the container.
Methods
-
keys()
-
Get all keys of the collection.
ReturnsType Description arrayAn array of keys.
items.keys();
-
has(key)
-
Checks if an item exists
ParametersName Type Description key stringThe key of the item to check.
ThrowsType Description TypeErrorIf
key
is not astring
.ReturnsType Description booltrue
if exists,false
otherwise.items.set('keyName', 'any value'); if(items.has('keyName')) { console.log('has `keyName`'); }
-
remove(key)
-
Remove an item.
ParametersName Type Description key stringThe key of item to remove.
ThrowsType Description TypeErrorIf
key
is not astring
.ReturnsType Description CollectionThe current ìnstance.
items.set('keyName', 'any value'); // true console.log(items.has('keyName')); items.remove('keyName'); // false console.log(items.has('keyName'));
-
set(key, value)
-
Set an item.
ParametersName Type Description key stringKey name.
value mixedThe value.
ThrowsType Description TypeErrorIf
key
is not astring
.ReturnsType Description CollectionThe current ìnstance.
items.set('hello', 'Hello World!'); // Hello World! console.log(items.get('hello'));
-
setAll(values)
-
Set all items of the collection. All collection is overwritten by the given set of items.
ParametersName Type Description values valuesThe new values of the collection.
ThrowsType Description TypeErrorIf
values
is not anobject
.ReturnsType Description CollectionThe current ìnstance.
items.setAll({ a: 'value 1', b: 'value 2', c: 'value 3' });
-
addAll(values)
-
Add all items in the collection.
items.addAll({ a: 'value 1', b: 'value 2', c: 'value 3' });
ParametersName Type Description values objectThe values to add. The existing values are overwritten, the other items are preserved.
ThrowsType Description TypeErrorIf
values
is not anobject
or if a key is not astring
.ReturnsType Description CollectionThe current ìnstance.
-
addOnce(key, value, skip_if_exists)
-
Add once an item in the collection. Identical to Collection.set() method, except the item is added only if it is not already defined in the collection.
ParametersName Type Argument Description key stringThe key name.
value mixedThe value.
skip_if_exists booloptional Defines the behavior if the given key exists:
- if
truthy
, nothing happens and the item already defined is not overwritten - if
falsy
(by default) anError
is thrown
ThrowsType Description TypeErrorIf
key
is not astring
.ErrorIf an item is already defined with the same
key
andskip_if_exists
is nottruthy
.ReturnsType Description CollectionThe current ìnstance.
See also - if
-
addOnceAll(values, skip_if_exists)
-
Add all items in the collection only if not already defined.
ParametersName Type Argument Description values objectIdentical to Collection.addAll() method.
skip_if_exists booloptional Defines the behavior if an item exists:
- if
truthy
, nothing happens and the item already defined is not overwritten - if
falsy
(by default) anError
is thrown
ThrowsType Description TypeErrorIf
values
is not anobject
or if a key is not astring
.ErrorIf an item is already defined and
skip_if_exists
is nottruthy
.ReturnsType Description CollectionThe current ìnstance.
See also - if
-
merge(values, values)
-
Merge
values
(recursive) in the collection.ParametersName Type Argument Description values objectValues to merge.
values objectoptional repeatable Zero, one or several other objects.
ThrowsType Description TypeErrorIf
values
is not anobject
.ReturnsType Description CollectionThe current ìnstance.
-
getAll()
-
Get all items of the collection.
ReturnsType Description objectAll items defined in the collection.
-
get(key, default_value, strict)
-
Get an item value.
If the item is a function, the fonction is called and
get()
returns the value returned by the function called.If you want the raw value, uses Collection.raw().
ParametersName Type Argument Description key stringThe key of the item
default_value mixedoptional The default value if the item does not exist (
default_value
is ignored ifstrict
istruthy
).strict booloptional If
truthy
and the item does not exist, throws anError
(default_value
is ignored ifstrict
istruthy
).ThrowsType Description TypeErrorIf
key
is not astring
.ErrorIf
strict
istruthy
and the item does not exist.ReturnsType Description - a default value is defined
strict
is nottruthy
- the item (
key
) does not exist
mixedThe item value (if defined).
mixedReturns
default_value
if:items.addAll({ a: 'value of "a"', b: function() { 'value of "b"' } }); // value of "a", string console.log(items.get('a'), typeof items.get('a')); // value of "b", string console.log(items.get('b'), typeof items.get('b')); // value of "a", string console.log(items.raw('a'), typeof items.raw('a')); // [Function], function console.log(items.raw('b'), typeof items.raw('b'));
See also -
raw(key, default_value, strict)
-
Get an item.
ParametersName Type Argument Description key stringThe key of the item
default_value mixedoptional The default value if the item does not exist (
default_value
is ignored ifstrict
istruthy
).strict booloptional If
truthy
and the item does not exist, throws anError
(default_value
is ignored ifstrict
istruthy
).ThrowsType Description TypeErrorIf
key
is not astring
.ErrorIf
strict
istruthy
and the item does not exist.ReturnsType Description - a default value is defined
strict
is nottruthy
- the item (
key
) does not exist
mixedThe item value (if defined).
mixedReturns
default_value
if:items.set('multiply', function(a, b) { return a * b; }); var multiply = items.raw('multiply'); // returns 8 multiply(2, 4); // or directly // returns 8 items.raw('multiply')(2, 4); // or equivalent by injection // returns 8 items.inject('multiply', function(multiply) { return multiply(2, 4); }); // returns 8 items.apply(function() { return this.multiply(2, 4); });
See also -
inject(deps, fn)
-
Call a function with dependencies injection.
Unlike
Collection.invoke()
, the raw value of each dependency is passed tofn
.ParametersName Type Argument Description deps stringarrayfunctionDependencies to inject as arguments of the function (
fn
). Or only a function that receives in first argument the container ofCollection
instance.fn functionoptional Function to call. Dependencies are passed as arguments in the order of declaration. If
deps
is a function, this argument is ignored.ThrowsType Description TypeErrorIf a key is not a
string
.ReturnsType Description mixedThe value returned by the given function.
items.set('multiply', function(a, b) { return a * b; }); // returns 8 items.inject('multiply', function(multiply) { return multiply(2, 4); }); // or equivalent // return 8 items.apply(function() { return this.multiply(2, 4); }); // returns 8 items.raw('multiply')(2, 4);
See also -
invoke(deps, fn)
-
Call a function with dependencies injection.
Unlike
Collection.inject()
, the returned value of each dependency is passed tofn
.ParametersName Type Argument Description deps stringarrayfunctionDependencies to call and inject the returned value as arguments of the function (
fn
). Or only a function that receives in first argument the container ofCollection
instance.fn functionoptional Function to call. Dependencies are passed as arguments in the order of declaration. If
deps
is a function, this argument is ignored.ThrowsType Description TypeErrorIf a key is not a
string
.ReturnsType Description mixedThe value returned by the given function.
items.addAll({ name: 'Nico', hello: function(){ return 'Hello ' + this.name; } }); items.invoke('hello', function(hello) { // Hello Nico console.log(hello); });
See also -
apply(bindable, arguments)
-
Calls a given function by binding the scope (
this
) to the container (Collection._container
).The javascript function implements natively the methods
call()
andapply()
.It is possible that
bindable
argument is not a function, in this case it is necessary that the object implementscall(container)
andapply(container, args)
, then handles the logic.ParametersName Type Argument Description bindable functionobjectFunction or object to call and whose the scope (
this
) will bind to container.arguments mixedoptional repeatable Zero, one or more arguments passed to
bindable
.ReturnsType Description mixedThe value returned by
bindable
.var fn = function() { return this === items._container; }; // true console.log(items.apply(fn));
See also -
wrap(value)
-
Wrap a value. Useful to avoid calling a function in the implementation of a provider or a factory.
ParametersName Type Description value mixedThe value to wrap.
ReturnsType Description functionvalue
wrapped by a function
See also -
singleton(key, fn)
-
Create a singleton (function shared).
ParametersName Type Description key stringThe key (function identifier).
fn functionThe function, executed once, after the value is returned when is again called.
ThrowsType Description TypeErrorIf
key
is not astring
or iffn
is not afunction
.ReturnsType Description CollectionThe current ìnstance.
See also -
provider(key, deps, fn)
-
Create a provider that supports dependencies injection. When the item
key
is called, it calls the functionfn
by passing dependenciesdeps
.This method defines a function that returns the result of Collection.inject(deps, fn).
ParametersName Type Argument Description key stringThe key (provider identifier).
deps stringarrayfunctionSee Collection.inject().
fn functionoptional See Collection.inject().
ThrowsType Description TypeErrorIf
key
is not astring
or if a given key indeps
is not astring
.ReturnsType Description CollectionThe current ìnstance.
items.set('hello', function() { return 'Hello World!'; }); items.provider('sayHello', ['hello'], function(hello) { return hello(); }); // 'Hello World!' items.get('sayHello'); // or with the scope in any injector items.apply(function() { // 'Hello World!' console.log(this.sayHello); });
See also -
factory(key, deps, fn)
-
Create a factory that supports dependencies injection. When the item
key
is called, it calls the functionfn
by passing dependenciesdeps
.This method defines a function that returns the result of Collection.invoke(deps, fn).
ParametersName Type Argument Description key stringThe key (factory identifier).
deps stringarrayfunctionSee
Collection.invoke()
.fn functionoptional See
Collection.invoke()
.ThrowsType Description TypeErrorIf
key
is not astring
or if a given key indeps
is not astring
.ReturnsType Description CollectionThe current ìnstance.
items.set('hello', function() { return 'Hello World!'; }); items.factory('sayHello', ['hello'], function(hello) { return hello; }); // 'Hello World!' items.get('sayHello'); // or with the scope in any injector items.apply(function() { // 'Hello World!' console.log(this.sayHello); });
See also