Interface Object

Index

Methods

Methods

public all(map: string): boolean

see

map

Parameters

  • map: string

Returns

boolean

public all(map: (key: string, value: any) => boolean): boolean

see

map

Parameters

  • map: (key: string, value: any) => boolean

Returns

boolean

public any(map: string): boolean

see

map

Parameters

  • map: string

Returns

boolean

public any(map: (key: string, value: any) => boolean): boolean

see

map

Parameters

  • map: (key: string, value: any) => boolean

Returns

boolean

public average(map: string): number

see

map

Parameters

  • map: string

Returns

number

public average(map: (key: string, value: any) => number): number

see

map

Parameters

  • map: (key: string, value: any) => number

Returns

number

public clone(deep?: boolean): any

Creates a clone (copy) of .

extra

Default is a shallow clone, unless [deep] is true. %clone% is available as an instance method on extended objects.

example

Object.clone({foo:'bar'}) -> { foo: 'bar' } Object.clone() -> {} Object.extended({foo:'bar'}).clone() -> { foo: 'bar' }

Parameters

  • deep?: boolean optional

    If true then deep clone, default = false.

Returns

any

Cloned object.

public count(map: string): number

see

map

Parameters

  • map: string

Returns

number

public count(map: (key: string, value: any) => boolean): number

see

map

Parameters

  • map: (key: string, value: any) => boolean

Returns

number

public equal(b: any): boolean

Returns true if and are equal.

extra

%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.

example

Object.equal({a:2}, {a:2}) -> true Object.equal({a:2}, {a:3}) -> false Object.extended({a:2}).equals({a:3}) -> false

Parameters

  • b: any

    Compare to this object

Returns

boolean

True if the objects are equal, otherwise false.

public extended(): Object

Creates a new object, equivalent to %new Object()% or %{}%, but with extended methods.

extra

See extended objects for more.

example

Object.extended() Object.extended({ happy:true, pappy:false }).keys() -> ['happy','pappy'] Object.extended({ happy:true, pappy:false }).values() -> [true, false]

Returns

Object

Extended object.

public find(map: string): any

see

map

Parameters

  • map: string

Returns

any

public find(map: (key: string, value: any) => boolean): any

see

map

Parameters

  • map: (key: string, value: any) => boolean

Returns

any

public findAll(map: string): Array<any>

see

map

Parameters

  • map: string

Returns

Array<any>

public findAll(map: (key: string, value: any) => boolean): Array<any>

see

map

Parameters

  • map: (key: string, value: any) => boolean

Returns

Array<any>

public has(key: string): boolean

Checks if has using hasOwnProperty from Object.prototype.

extra

This method is considered safer than %Object#hasOwnProperty% when using objects as hashes. See http://www.devthought.com/2012/01/18/an-object-is-not-a-hash/ for more.

example

Object.has({ foo: 'bar' }, 'foo') -> true Object.has({ foo: 'bar' }, 'baz') -> false Object.has({ hasOwnProperty: true }, 'foo') -> false

Parameters

  • key: string

    Property to check to see if it exists on .

Returns

boolean

True if exists on , otherwise false.

public isArray(): boolean

Returns true if is an object of that type.

method

Object.isType

extra

%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".

set

isArray isObject isBoolean isDate isFunction isNaN isNumber isString isRegExp

example

Object.isArray([1,2,3]) -> true Object.isDate(3) -> false Object.isRegExp(/wasabi/) -> true Object.isObject({ broken:'wear' }) -> true

Returns

boolean

True if is of type [Type].

public isBoolean(): boolean

see

isArray

Returns

boolean

public isDate(): boolean

see

isArray

Returns

boolean

public isEmpty(): boolean

see

map

Returns

boolean

public isFunction(): boolean

see

isArray

Returns

boolean

public isNaN(): boolean

see

isArray

Returns

boolean

public isNumber(): boolean

see

isArray

Returns

boolean

public isObject(): boolean

see

isArray

Returns

boolean

public isRegExp(): boolean

see

isArray

Returns

boolean

public isString(): boolean

see

isArray

Returns

boolean

public keys(fn?: (key: string, value: any) => void): Array<string>

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

  • fn?: (key: string, value: any) => void optional

    Optional callback for each key/value pair.

Returns

Array<string>

Array of each property on .

public least(map: string): any

see

map

Parameters

  • map: string

Returns

any

public least(map: (key: string, value: any) => any): any

see

map

Parameters

  • map: (key: string, value: any) => any

Returns

any

public map(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 .

method

enumerable

extra

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

example

Object.any({foo:'bar'}, 'bar') -> true Object.extended({foo:'bar'}).any('bar') -> true Object.isEmpty({}) -> true Object.map({ fred: { age: 52 } }, 'age'); -> { fred: 52 }

Parameters

  • map: string

Returns

U

Boolean

public map(map: (key: string, value: any) => any): U

see

map

Parameters

  • map: (key: string, value: any) => any

Returns

U

public max(map: string): any

see

map

Parameters

  • map: string

Returns

any

public max(map: (key: string, value: any) => any): any

see

map

Parameters

  • map: (key: string, value: any) => any

Returns

any

public merge(source: any, deep?: boolean, resolve?: boolean): any

Merges all the properties of into .

extra

Merges are shallow unless [deep] is %true%. Properties of will win in the case of conflicts, unless [resolve] is %false%. [resolve] can also be a function that resolves the conflict. In this case it will be passed 3 arguments, %key%, %targetVal%, and %sourceVal%, with the context set to . This will allow you to solve conflict any way you want, ie. adding two numbers together, etc. %merge% is available as an instance method on extended objects.

example

Object.merge({a:1},{b:2}) -> { a:1, b:2 } Object.merge({a:1},{a:2}, false, false) -> { a:1 }

  • Object.merge({a:1},{a:2}, false, function(key, a, b) { return a + b; }); -> { a:3 } Object.extended({a:1}).merge({b:2}) -> { a:1, b:2 }

Parameters

  • source: any
  • deep?: boolean optional

    If true then deep merge, default = false.

  • resolve?: boolean optional

    If false then properties of source are used in the merge, default = true.

Returns

any

Merged object

public merge(source: any, deep?: boolean, resolve?: (key: string, targetVal: any, sourceVal: any) => any): any

see

merge

Parameters

  • source: any
  • deep?: boolean optional
  • resolve?: (key: string, targetVal: any, sourceVal: any) => any optional

    Callback to resolve conflicts in the merge.

Returns

any

public min(map: string): any

see

map

Parameters

  • map: string

Returns

any

public min(map: (key: string, value: any) => any): any

see

map

Parameters

  • map: (key: string, value: any) => any

Returns

any

public most(map: string): any

see

map

Parameters

  • map: string

Returns

any

public most(map: (key: string, value: any) => any): any

see

map

Parameters

  • map: (key: string, value: any) => any

Returns

any

public none(map: string): boolean

see

map

Parameters

  • map: string

Returns

boolean

public none(map: (key: string, value: any) => boolean): boolean

see

map

Parameters

  • map: (key: string, value: any) => boolean

Returns

boolean

public reduce(map: string, init?: any): any

see

map

Parameters

  • map: string
  • init?: any optional

Returns

any

public reduce(map: (key: string, value: any) => any, init?: any): any

see

map

Parameters

  • map: (key: string, value: any) => any
  • init?: any optional

Returns

any

public reject(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

  • 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(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

  • 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(): number

Returns the number of properties in .

extra

%size% is available as an instance method on extended objects.

example

Object.size({ foo: 'bar' }) -> 1

Returns

number

The number of properties on .

public sum(map: string): number

see

map

Parameters

  • map: string

Returns

number

public sum(map: (key: string, value: any) => number): number

see

map

Parameters

  • map: (key: string, value: any) => number

Returns

number

public tap(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().

example

Object.extend(); [2,4,6].map(Math.exp).tap(function(arr) { arr.pop() }); [2,4,6].map(Math.exp).tap('pop').map(Math.round); -> [7,55]

Parameters

  • fn: string

    Function to tap on .

Returns

any

public tap(fn: (...args: any[]) => any): any

see

tap

Parameters

  • fn: (...args: any[]) => any

Returns

any

public toQueryString(namespace?: string): string

Converts the object into a query string. Accepts deep nested objects and arrays. If namespace is passed, it will be prefixed to all param names.

Parameters

  • namespace?: string optional

    Namespace to prefix properties with in the query string.

Returns

string

A query string generated from obj and namespace.

public values(fn?: (value: any) => any): Array<any>

Returns an array containing the values in . Optionally calls [fn] for each value.

extra

Returned values are in no particular order. %values% is available as an instance method on extended objects.

example

Object.values({ broken: 'wear' }) -> ['wear'] Object.values({ broken: 'wear' }, function(value) { // Called once for each value. }); Object.extended({ broken: 'wear' }).values() -> ['wear']

Parameters

  • fn?: (value: any) => any optional

    Optional callback for each value on .

Returns

Array<any>

Array

public watch(prop: string, fn: (prop?: string, oldVal?: any, newVal?: any) => any)

Watches a property of and runs when it changes.

extra

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

  • prop: string

    Property to watch on

  • fn: (prop?: string, oldVal?: any, newVal?: any) => any

    Callback function when changes on .