common | angular-ui-router
Options
Menu

Module common

Random utility functions used in the UI-Router code

These functions are exported, but are subject to change without notice.

Index

Type aliases

IInjectable: Function | Array<any>

An ng1-style injectable

An ng1-style injectable

This could be a (non-minified) function such as:

function injectableFunction(SomeDependency) {

}

or an explicitly annotated function (minify safe)

injectableFunction.$inject = [ 'SomeDependency' ];
function injectableFunction(SomeDependency) {

}

or an array style annotated function (minify safe)

['SomeDependency', function injectableFunction(SomeDependency) {

}];
publicapi
Mapper: function

Type declaration

    • (x: X, key?: string | number): T
    • Parameters

      • x X
      • key: Optional  string | number

      Returns T

PickOmitPredicate: function

Type declaration

    • (keys: string[], key: string): boolean
    • Parameters

      • keys string[]
      • key string

      Returns boolean

Predicate: function

Type declaration

    • (x?: X): boolean
    • Parameters

      • x: Optional  X

      Returns boolean

sortfn: function

Type declaration

    • (a: any, b: any): number
    • Parameters

      • a any
      • b any

      Returns number

Variables

angular: any = w.angular || {}
assertMap: function = assertFn

Given a .map function, builds a .map function which throws an error if any mapped elements do not pass a truthyness test.

Given a .map function, builds a .map function which throws an error if any mapped elements do not pass a truthyness test.

example

var data = { foo: 1, bar: 2 };

let keys = [ 'foo', 'bar' ]
let values = keys.map(assertMap(key => data[key], "Key not found"));
// values is [1, 2]

let keys = [ 'foo', 'bar', 'baz' ]
let values = keys.map(assertMap(key => data[key], "Key not found"));
// throws Error("Key not found")

Type declaration

    • <T, U>(mapFn: function, errMsg: string | Function): function
    • Type parameters

      • T

      • U

      Parameters

      • mapFn function
          • (t: T): U
          • Parameters

            • t T

            Returns U

      • errMsg string | Function

      Returns function

        • (t: T): U
        • Parameters

          • t T

          Returns U

assertPredicate: function = assertFn

Given a .filter Predicate, builds a .filter Predicate which throws an error if any elements do not pass.

Given a .filter Predicate, builds a .filter Predicate which throws an error if any elements do not pass.

example

let isNumber = (obj) => typeof(obj) === 'number';
let allNumbers = [ 1, 2, 3, 4, 5 ];
allNumbers.filter(assertPredicate(isNumber)); //OK

let oneString = [ 1, 2, 3, 4, "5" ];
oneString.filter(assertPredicate(isNumber, "Not all numbers")); // throws Error(""Not all numbers"");

Type declaration

copy: any = angular.copy || _copy
equals: any = angular.equals || _equals
extend: any = angular.extend || _extend
forEach: any = angular.forEach || _forEach
fromJson: any = angular.fromJson || JSON.parse.bind(JSON)
inArray: _inArray = curry(_inArray) as any

Given an array, returns true if the object is found in the array, (using indexOf)

Given an array, returns true if the object is found in the array, (using indexOf)

mapObj: function = map

Given an object, returns a new object, where each property is transformed by the callback function

Given an object, returns a new object, where each property is transformed by the callback function

Type declaration

    • <T, U>(collection: object, callback: Mapper): object
    • Type parameters

      • T

      • U

      Parameters

      • collection object
        • [key: string]: T
      • callback Mapper

      Returns object

      • [key: string]: U
pushTo: _pushTo = curry(_pushTo) as any

pushes a values to an array and returns the value

pushes a values to an array and returns the value

removeFrom: _removeFrom = curry(_removeFrom) as any

Given an array, and an item, if the item is found in the array, it removes it (in-place). The same array is returned

Given an array, and an item, if the item is found in the array, it removes it (in-place). The same array is returned

toJson: any = angular.toJson || JSON.stringify.bind(JSON)
w: any = typeof window === 'undefined' ? {} : window

Functions

  • _arraysEq(a1: any[], a2: any[]): any
  • _copy(src: Obj, dest: Obj): any
  • shallow copy from src to dest

  • shallow copy from src to dest

    note: This is a shallow copy, while angular.copy is a deep copy. ui-router uses copy only to make copies of state parameters.

    Parameters

    Returns any


  • _equals(o1: any, o2: any): boolean
  • _forEach(obj: any, cb: function, _this: Obj): void
  • Naive forEach implementation works with Objects or Arrays

  • Naive forEach implementation works with Objects or Arrays

    Parameters

    • obj any
    • cb function
        • (el: any, idx?: any): void
        • Parameters

          • el any
          • idx: Optional  any

          Returns void

    • _this Obj

    Returns void


  • _inArray(array: any[], obj: any): boolean
  • _inArray(array: any[]): function
  • _pushTo<T>(arr: T[], val: T): T
  • _pushTo<T>(arr: T[]): function
  • _removeFrom<T>(array: T[], obj: T): T[]
  • _removeFrom<T>(array: T[]): function
  • allTrueR(memo: boolean, elem: any): any
  • Reduce function that returns true if all of the values are truthy.

  • ancestors(first: State, second: State): Array<State>
  • Finds the common ancestor path between two states.

  • Finds the common ancestor path between two states.

    Parameters

    • first State
      :

      The first state.

    • second State
      :

      The second state.

    Returns Array<State>

    :

    Returns an array of state names in descending order, not including the root.


  • anyTrueR(memo: boolean, elem: any): any
  • Reduce function that returns true if any of the values are truthy.

  • Reduce function that returns true if any of the values are truthy.

    • @example ```

    let vals = [ 0, null, undefined ]; vals.reduce(anyTrueR, true); // false

    vals.push("hello world"); vals.reduce(anyTrueR, true); // true ```

    Parameters

    • memo boolean
    • elem any

    Returns any


  • applyPairs(memo: TypedMap<any>, keyValTuple: any[]): TypedMap<any>
  • Reduce function which builds an object from an array of [key, value] pairs.

  • Reduce function which builds an object from an array of [key, value] pairs.

    Each iteration sets the key/val pair on the memo object, then returns the memo for the next iteration.

    Each keyValueTuple should be an array with values [ key: string, value: any ]

    example
    
    var pairs = [ ["fookey", "fooval"], ["barkey", "barval"] ]
    
    var pairsToObj = pairs.reduce((memo, pair) => applyPairs(memo, pair), {})
    // pairsToObj == { fookey: "fooval", barkey: "barval" }
    
    // Or, more simply:
    var pairsToObj = pairs.reduce(applyPairs, {})
    // pairsToObj == { fookey: "fooval", barkey: "barval" }
    

    Parameters

    Returns TypedMap<any>


  • arrayTuples(...arrayArgs: any[]): any[]
  • Given two or more parallel arrays, returns an array of tuples where each tuple is composed of [ a[i], b[i], ... z[i] ]

  • Given two or more parallel arrays, returns an array of tuples where each tuple is composed of [ a[i], b[i], ... z[i] ]

    example
    
    let foo = [ 0, 2, 4, 6 ];
    let bar = [ 1, 3, 5, 7 ];
    let baz = [ 10, 30, 50, 70 ];
    arrayTuples(foo, bar);       // [ [0, 1], [2, 3], [4, 5], [6, 7] ]
    arrayTuples(foo, bar, baz);  // [ [0, 1, 10], [2, 3, 30], [4, 5, 50], [6, 7, 70] ]
    

    Parameters

    • ...arrayArgs: Rest  any[]

    Returns any[]


  • assertFn(predicateOrMap: Function, errMsg?: string | Function): any
  • composeSort(...sortFns: sortfn[]): sortfn
  • Composes a list of sort functions

  • Composes a list of sort functions

    Creates a sort function composed of multiple sort functions. Each sort function is invoked in series. The first sort function to return non-zero "wins".

    Parameters

    • ...sortFns: Rest  sortfn[]
      :

      list of sort functions

    Returns sortfn


  • createProxyFunctions(source: Function, target: Obj, bind: Function, fnNames?: string[], latebind?: boolean): Obj
  • Builds proxy functions on the to object which pass through to the from object.

  • Builds proxy functions on the to object which pass through to the from object.

    For each key in fnNames, creates a proxy function on the to object. The proxy function calls the real function on the from object.

    Example:

    This example creates an new class instance whose functions are prebound to the new'd object.

    class Foo {
      constructor(data) {
        // Binds all functions from Foo.prototype to 'this',
        // then copies them to 'this'
        bindFunctions(Foo.prototype, this, this);
        this.data = data;
      }
    
      log() {
        console.log(this.data);
      }
    }
    
    let myFoo = new Foo([1,2,3]);
    var logit = myFoo.log;
    logit(); // logs [1, 2, 3] from the myFoo 'this' instance
    

    Example:

    This example creates a bound version of a service function, and copies it to another object

    
    var SomeService = {
      this.data = [3, 4, 5];
      this.log = function() {
        console.log(this.data);
      }
    }
    
    // Constructor fn
    function OtherThing() {
      // Binds all functions from SomeService to SomeService,
      // then copies them to 'this'
      bindFunctions(SomeService, this, SomeService);
    }
    
    let myOtherThing = new OtherThing();
    myOtherThing.log(); // logs [3, 4, 5] from SomeService's 'this'
    

    Parameters

    • source Function
      :

      A function that returns the source object which contains the original functions to be bound

    • target Obj
      :

      A function that returns the target object which will receive the bound functions

    • bind Function
      :

      A function that returns the object which the functions will be bound to

    • fnNames: Optional  string[]
      :

      The function names which will be bound (Defaults to all the functions found on the 'from' object)

    • latebind: Default value  boolean = false
      :

      If true, the binding of the function is delayed until the first time it's invoked

    Returns Obj


  • defaults(opts?: object, ...defaultsList: Obj[]): any
  • Applies a set of defaults to an options object. The options object is filtered to only those properties of the objects in the defaultsList. Earlier objects in the defaultsList take precedence when applying defaults.

  • Applies a set of defaults to an options object. The options object is filtered to only those properties of the objects in the defaultsList. Earlier objects in the defaultsList take precedence when applying defaults.

    Parameters

    • opts: Default value  object = {}
    • ...defaultsList: Rest  Obj[]

    Returns any


  • deregAll(functions: Function[]): void
  • Given an array of (deregistration) functions, calls all functions and removes each one from the source array

  • Given an array of (deregistration) functions, calls all functions and removes each one from the source array

    Parameters

    • functions Function[]

    Returns void


  • filter<T>(collection: T[], callback: function): T[]
  • Given an array of objects, returns a new array containing only the elements which passed the callback predicate

  • filter<T>(collection: TypedMap<T>, callback: function): TypedMap<T>
  • Given an object, returns a new object with only those properties that passed the callback predicate

  • Given an array of objects, returns a new array containing only the elements which passed the callback predicate

    Type parameters

    • T

    Parameters

    • collection T[]
    • callback function
        • (t: T, key?: number): boolean
        • Parameters

          • t T
          • key: Optional  number

          Returns boolean

    Returns T[]


  • Given an object, returns a new object with only those properties that passed the callback predicate

    Type parameters

    • T

    Parameters

    • collection TypedMap<T>
    • callback function
        • (t: T, key?: string): boolean
        • Parameters

          • t T
          • key: Optional  string

          Returns boolean

    Returns TypedMap<T>


  • find<T>(collection: TypedMap<T>, callback: Predicate): T
  • Given an object, return the first property of that object which passed the callback predicate

  • find<T>(collection: T[], callback: Predicate): T
  • Given an array of objects, returns the first object which passed the callback predicate

  • flatten(arr: any[]): any
  • Return a completely flattened version of an array.

  • flattenR(memo: any[], elem: any): any
  • Reduce function which recursively un-nests all arrays

  • identity(x: any): any
  • inherit(parent: Obj, extra: Obj): any
  • prototypal inheritance helper. Creates a new object which has parent object as its prototype, and then copies the properties from extra onto it

  • prototypal inheritance helper. Creates a new object which has parent object as its prototype, and then copies the properties from extra onto it

    Parameters

    Returns any


  • map<T, U>(collection: T[], callback: Mapper): U[]
  • Given an array, returns a new array, where each element is transformed by the callback function

  • map<T, U>(collection: object, callback: Mapper): object
  • Maps an array or object properties using a callback function

  • merge(dst: Obj, ...objs: Obj[]): Obj
  • Merges properties from the list of objects to the destination object. If a property already exists in the destination object, then it is not overwritten.

  • Merges properties from the list of objects to the destination object. If a property already exists in the destination object, then it is not overwritten.

    Parameters

    • dst Obj
    • ...objs: Rest  Obj[]

    Returns Obj


  • mergeR(memo: Obj, item: Obj): any
  • Reduce function that merges each element of the list into a single object, using extend

  • noop(): any
  • notImplemented(fnname: string): (Anonymous function)
  • omit(obj: Obj, propNames: string[]): Obj
  • Return a copy of the object omitting the blacklisted properties.

  • omit(obj: Obj, ...propNames: string[]): Obj
  • Return a copy of the object omitting the blacklisted properties.

  • Return a copy of the object omitting the blacklisted properties.

    example
    
    var foo = { a: 1, b: 2, c: 3 };
    var ab = omit(foo, ['a', 'b']); // { c: 3 }
    

    Parameters

    • obj Obj
      :

      the source object

    • propNames string[]
      :

      an Array of strings, which are the blacklisted property names

    Returns Obj


  • Return a copy of the object omitting the blacklisted properties.

    example
    
    var foo = { a: 1, b: 2, c: 3 };
    var ab = omit(foo, 'a', 'b'); // { c: 3 }
    

    Parameters

    • obj Obj
      :

      the source object

    • ...propNames: Rest  string[]
      :

      1..n strings, which are the blacklisted property names

    Returns Obj


  • pairs(obj: Obj): Array<Array<any>>
  • Like _.pairs: Given an object, returns an array of key/value pairs

  • pick(obj: Obj, propNames: string[]): Obj
  • Return a copy of the object only containing the whitelisted properties.

  • pick(obj: Obj, ...propNames: string[]): Obj
  • Return a copy of the object only containing the whitelisted properties.

  • Return a copy of the object only containing the whitelisted properties.

    example
    
    var foo = { a: 1, b: 2, c: 3 };
    var ab = pick(foo, ['a', 'b']); // { a: 1, b: 2 }
    

    Parameters

    • obj Obj
      :

      the source object

    • propNames string[]
      :

      an Array of strings, which are the whitelisted property names

    Returns Obj


  • Return a copy of the object only containing the whitelisted properties.

    example
    
    var foo = { a: 1, b: 2, c: 3 };
    var ab = pick(foo, 'a', 'b'); // { a: 1, b: 2 }
    

    Parameters

    • obj Obj
      :

      the source object

    • ...propNames: Rest  string[]
      :

      1..n strings, which are the whitelisted property names

    Returns Obj


  • pluck(collection: Obj[], propName: string): Obj[]
  • Given an array of objects, maps each element to a named property of the element.

  • pluck(collection: object, propName: string): object
  • Given an object, maps each property of the object to a named property of the property.

  • Given an array of objects, maps each element to a named property of the element.

    Parameters

    • collection Obj[]
    • propName string

    Returns Obj[]


  • Given an object, maps each property of the object to a named property of the property.

    Parameters

    • collection object
      • [key: string]: any
    • propName string

    Returns object

    • [key: string]: any

  • pushR(arr: any[], obj: any): Array<any>
  • Reduce function that pushes an object to an array, then returns the array. Mostly just for flattenR and uniqR

  • restArgs(args: IArguments, idx?: number): any
  • Given an arguments object, converts the arguments at index idx and above to an array. This is similar to es6 rest parameters.

  • Given an arguments object, converts the arguments at index idx and above to an array. This is similar to es6 rest parameters.

    Optionally, the argument at index idx may itself already be an array.

    For example, given either: arguments = [ obj, "foo", "bar" ] or: arguments = [ obj, ["foo", "bar"] ] then: restArgs(arguments, 1) == ["foo", "bar"]

    This allows functions like pick() to be implemented such that it allows either a bunch of string arguments (like es6 rest parameters), or a single array of strings:

    given: var obj = { foo: 1, bar: 2, baz: 3 }; then: pick(obj, "foo", "bar"); // returns { foo: 1, bar: 2 } pick(obj, ["foo", "bar"]); // returns { foo: 1, bar: 2 }

    Parameters

    • args IArguments
    • idx: Default value  number = 0

    Returns any


  • silenceUncaughtInPromise(promise: Promise<any>): Promise<any>
  • silentRejection(error: any): Promise<any>
  • sortBy(propFn: function, checkFn?: Predicate): (Anonymous function)
  • Create a sort function

  • Create a sort function

    Creates a sort function which sorts by a numeric property.

    The propFn should return the property as a number which can be sorted.

    Example:

    This example returns the priority prop.

    var sortfn = sortBy(obj => obj.priority)
    // equivalent to:
    var longhandSortFn = (a, b) => a.priority - b.priority;
    

    Example:

    This example uses prop

    var sortfn = sortBy(prop('priority'))
    

    The checkFn can be used to exclude objects from sorting.

    Example:

    This example only sorts objects with type === 'FOO'

    var sortfn = sortBy(prop('priority'), propEq('type', 'FOO'))
    

    Parameters

    • propFn function
      :

      a function that returns the property (as a number)

        • (a: any): number
        • Parameters

          • a any

          Returns number

    • checkFn: Default value  Predicate = val(true)
      :

      a predicate

    Returns (Anonymous function)

    :

    a sort function like: (a, b) => (checkFn(a) && checkFn(b)) ? propFn(a) - propFn(b) : 0


  • tail<T>(arr: T[]): T
  • Get the last element of an array

  • uniqR<T>(acc: T[], token: T): T[]
  • Reduce function that filters out duplicates

  • unnest(arr: any[]): any
  • Return a new array with a single level of arrays unnested.

  • unnestR(memo: any[], elem: any[]): Array<any>
  • Reduce function which un-nests a single level of arrays

  • values(obj: Obj): Array<any>
  • Given an object, return its enumerable property values

Object literals

services: object
$injector: undefined = undefined
$q: undefined = undefined

Generated using TypeDoc