%equal% in Sugar is "egal", meaning the values are equal
if they are "not observably distinguishable". Note that on
extended objects the name is %equals% for readability.
%isObject% will return false on anything that is not an object
literal, including instances of inherited classes. Note also
that %isNaN% will ONLY return true if the object IS %NaN%.
It does not mean the same as browser native %isNaN%, which returns
true for anything that is "not a number".
Returns an array containing the keys in . Optionally calls [fn] for each key.
extra
This method is provided for browsers that don't support it natively,
and additionally is enhanced to accept the callback [fn]. Returned
keys are in no particular order. %keys% is available as an instance
method on extended objects.
example
Object.keys({ broken: 'wear' }) -> ['broken']
Object.keys({ broken: 'wear' }, function(key, value) {
// Called once for each key.
});
Object.extended({ broken: 'wear' }).keys() -> ['broken']
Parameters
obj: any
Retreive all keys/properties from this object.
fn?: (key: string, value: any) => voidoptional
Optional callback for each key/value pair.
Returns
Array<string>
Array of each property on .
public least(obj: any, map: string): any
see
map
Parameters
obj: any
map: string
Returns
any
public least(obj: any, map: (key: string, value: any) => any): any
see
map
Parameters
obj: any
map: (key: string, value: any) => any
Returns
any
public map(obj: T, map: string): U
Enumerable methods in the Array package are also available to
the Object class. They will perform their normal operations for
every property in .
In cases where a callback is used, instead of %element, index%,
the callback will instead be passed %key, value%. Enumerable methods
are also available to extended objects as instance methods.
set
each
map
any
all
none
count
find
findAll
reduce
isEmpty
sum
average
min
max
least
most
public reduce(obj: any, map: string, init?: any): any
see
map
Parameters
obj: any
map: string
init?: anyoptional
Returns
any
public reduce(obj: any, map: (key: string, value: any) => any, init?: any): any
see
map
Parameters
obj: any
map: (key: string, value: any) => any
init?: anyoptional
Returns
any
public reject(obj: any, find?: Array<any>): any
Builds a new object containing all values except those specified in find.
When find is a string, that single key will be rejected. It can also be a regex,
rejecting any key that matches, or an object which will match if the key also
exists in that object, effectively "subtracting" that object. Multiple selections
may also be passed as an array or directly as enumerated arguments. reject is
available as an instance method on extended objects.
Parameters
obj: any
Object to remove the properties in find.
find?: Array<any>optional
The property (string), properties (object) or RegExp to remove from obj.
Returns
any
Modified obj with find properties removed.
public select(obj: any, find?: Array<any>): any
Builds a new object containing the values specified in find. When find is a string,
that single key will be selected. It can also be a regex, selecting any key that
matches, or an object which will match if the key also exists in that object,
effectively doing an "intersect" operation on that object. Multiple selections
may also be passed as an array or directly as enumerated arguments. select is
available as an instance method on extended objects.
Parameters
obj: any
Object to keep the properties in find.
find?: Array<any>optional
The property (string), properties (object) or RegExp to keep on obj.
Returns
any
Modified obj with only the find properties remaining.
public size(obj: Object): number
Returns the number of properties in .
extra
%size% is available as an instance method on extended objects.
example
Object.size({ foo: 'bar' }) -> 1
Parameters
obj: Object
Object to determine the number of properties on.
Returns
number
The number of properties on .
public sum(obj: any, map: string): number
see
map
Parameters
obj: any
map: string
Returns
number
public sum(obj: any, map: (key: string, value: any) => number): number
see
map
Parameters
obj: any
map: (key: string, value: any) => number
Returns
number
public tap(obj: any, fn: string): any
Runs and returns .
extra
A string can also be used as a shortcut to a method. This method
is used to run an intermediary function in the middle of method
chaining. As a standalone method on the Object class it doesn't
have too much use. The power of %tap% comes when using extended
objects or modifying the Object prototype with Object.extend().
is passed three arguments: the property , the old value,
and the new value. The return value of [fn] will be set as the new
value. This method is useful for things such as validating or cleaning
the value when it is set. Warning: this method WILL NOT work in
browsers that don't support %Object.defineProperty%. This notably
includes IE 8 and below, and Opera. This is the only method in Sugar
that is not fully compatible with all browsers. %watch% is available
as an instance method on extended objects.
example
Object.watch({ foo: 'bar' }, 'foo', function(prop, oldVal, newVal) {
// Will be run when the property 'foo' is set on the object.
});
Object.extended().watch({ foo: 'bar' }, 'foo', function(prop, oldVal, newVal) {
// Will be run when the property 'foo' is set on the object.
});
Parameters
obj: any
Object to watch.
prop: string
Property to watch on
fn: (prop?: string, oldVal?: any, newVal?: any) => any