Interface ObjectStatic

Index

Methods

Methods

public all(obj: any, map: string): boolean

see

map

Parameters

  • obj: any
  • map: string

Returns

boolean

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

see

map

Parameters

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

Returns

boolean

public any(obj: any, map: string): boolean

see

map

Parameters

  • obj: any
  • map: string

Returns

boolean

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

see

map

Parameters

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

Returns

boolean

public average(obj: any, map: string): number

see

map

Parameters

  • obj: any
  • map: string

Returns

number

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

see

map

Parameters

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

Returns

number

public clone(obj?: T, deep?: boolean): T

Creates a clone (copy) of .

method

clone( = {}, [deep] = false)

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

  • obj?: T optional

    Object to clone.

  • deep?: boolean optional

    If true then deep clone, default = false.

Returns

T

Cloned object.

public count(obj: any, map: string): number

see

map

Parameters

  • obj: any
  • map: string

Returns

number

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

see

map

Parameters

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

Returns

number

public equal(a: any, 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

  • a: any

    First object to compare.

  • b: any

    Second object to compare.

Returns

boolean

True if the objects are equal, otherwise false.

public extended(obj?: any): any

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]

Parameters

  • obj?: any optional

    , default = {}.

Returns

any

Extended object.

public find(obj: any, map: string): any

see

map

Parameters

  • obj: any
  • map: string

Returns

any

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

see

map

Parameters

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

Returns

any

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

see

map

Parameters

  • obj: any
  • map: string

Returns

Array<any>

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

see

map

Parameters

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

Returns

Array<any>

public fromQueryString(str: string, deep?: boolean): T

Converts the query string of a URL into an object.

method

Object.fromQueryString(, [deep] = true)

extra

If [deep] is %false%, conversion will only accept shallow params (ie. no object or arrays with %[]% syntax) as these are not universally supported.

example

Object.fromQueryString('foo=bar&broken=wear') -> { foo: 'bar', broken: 'wear' } Object.fromQueryString('foo[]=1&foo[]=2') -> { foo: [1,2] }

Parameters

  • str: string

    Query string to convert into an object.

  • deep?: boolean optional

    If false the conversion will only accept shallow params, default = true.

Returns

T

Object generated from the .

public has(obj: any, 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

  • obj: any

    Object to check for the property .

  • key: string

    Property to check to see if it exists on .

Returns

boolean

True if exists on , otherwise false.

public isArray(obj: any): 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

Parameters

  • obj: any

    Object to check if it is of type [Type].

Returns

boolean

True if is of type [Type].

public isBoolean(obj: any): boolean

see

isArray

Parameters

  • obj: any

Returns

boolean

public isDate(obj: any): boolean

see

isArray

Parameters

  • obj: any

Returns

boolean

public isEmpty(obj: any): boolean

see

map

Parameters

  • obj: any

Returns

boolean

public isFunction(obj: any): boolean

see

isArray

Parameters

  • obj: any

Returns

boolean

public isNaN(obj: any): boolean

see

isArray

Parameters

  • obj: any

Returns

boolean

public isNumber(obj: any): boolean

see

isArray

Parameters

  • obj: any

Returns

boolean

public isObject(obj: any): boolean

see

isArray

Parameters

  • obj: any

Returns

boolean

public isRegExp(obj: any): boolean

see

isArray

Parameters

  • obj: any

Returns

boolean

public isString(obj: any): boolean

see

isArray

Parameters

  • obj: any

Returns

boolean

public keys(obj: any, 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

  • obj: any

    Retreive all keys/properties from this object.

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

    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 .

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

  • obj: T
  • map: string

Returns

U

Boolean

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

see

map

Parameters

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

Returns

U

public max(obj: any, map: string): any

see

map

Parameters

  • obj: any
  • map: string

Returns

any

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

see

map

Parameters

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

Returns

any

public merge(target: any, 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

  • target: any
  • 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(target: any, source: any, deep?: boolean, resolve?: (key: string, targetVal: any, sourceVal: any) => any): any

see

merge

Parameters

  • target: any
  • 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(obj: any, map: string): any

see

map

Parameters

  • obj: any
  • map: string

Returns

any

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

see

map

Parameters

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

Returns

any

public most(obj: any, map: string): any

see

map

Parameters

  • obj: any
  • map: string

Returns

any

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

see

map

Parameters

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

Returns

any

public none(obj: any, map: string): boolean

see

map

Parameters

  • obj: any
  • map: string

Returns

boolean

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

see

map

Parameters

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

Returns

boolean

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

see

map

Parameters

  • obj: any
  • map: string
  • init?: any optional

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?: any optional

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

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

  • obj: any

    Object to tap.

  • fn: string

    Function to tap on .

Returns

any

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

see

tap

Parameters

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

Returns

any

public toQueryString(obj: any, 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

  • obj: any

    Object to convert to a query string.

  • namespace?: string optional

    Namespace to prefix properties with in the query string.

Returns

string

A query string generated from obj and namespace.

public values(obj: any, 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

  • obj: any

    Object to retrieve all values from.

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

    Optional callback for each value on .

Returns

Array<any>

Array

public watch(obj: any, 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

  • obj: any

    Object to watch.

  • prop: string

    Property to watch on

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

    Callback function when changes on .