common | @uirouter/angularjs
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 | 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

Predicate: function

Type declaration

    • (x?: X): boolean
    • Parameters

      • x: Optional  X

      Returns boolean

Variables

angular: any = root.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

equals: any = angular.equals || _equals
extend: assign = Object.assign || _extend
forEach: any = angular.forEach || _forEach
fromJson: any = angular.fromJson || JSON.parse.bind(JSON)
global: any
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<T, U>, target?: object): object
    • Type parameters

      • T

      • U

      Parameters

      • collection object
        • [key: string]: T
      • callback Mapper<T, U>
      • target: Optional  object
        • [key: string]: T

      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

root: any = (typeof self === 'object' && self.self === self && self) ||(typeof global === 'object' && global.global === global && global) ||this
toJson: any = angular.toJson || JSON.stringify.bind(JSON)

Functions

  • _arraysEq(a1: any[], a2: any[]): any
  • _equals(o1: any, o2: any): boolean
  • _extend(toObj: Obj, ...fromObjs: Obj[]): any
  • Like Object.assign()

  • _forEach(obj: any[] | 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[] | 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: StateObject, second: StateObject): StateObject[]
  • Finds the common ancestor path between two states.

  • 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(...args: 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

    • ...args: Rest  any[]

    Returns any[]


  • assertFn(predicateOrMap: Function, errMsg?: string | Function): any
  • copy(src: Obj, dest?: Obj): Obj
  • shallow copy from src to dest

  • 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: any, ...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 any
    • ...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>): T
  • Given an object, return the first property of that object which passed the callback predicate

  • find<T>(collection: T[], callback: Predicate<T>): 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

    • parent Obj
    • extra: Optional  Obj

    Returns any


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

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

  • Given an array, returns a new array, where each element is transformed by the callback function

    Type parameters

    • T

    • U

    Parameters

    • collection T[]
    • callback Mapper<T, U>
    • target: Optional  Array

    Returns U[]


  • Maps an array or object properties using a callback function

    Type parameters

    • T

    • U

    Parameters

    • collection object
      • [key: string]: T
    • callback Mapper<T, U>
    • target: Optional  object
      • [key: string]: T

    Returns object

    • [key: string]: U

  • mergeR(memo: Obj, item: Obj): Obj
  • 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.

  • 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


  • pairs(obj: Obj): 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.

  • 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


  • pluck<T>(collection: Obj[], propName: string): T[]
  • 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.

    Type parameters

    • T

    Parameters

    • collection Obj[]
    • propName string

    Returns T[]


  • 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): any[]
  • Reduce function that pushes an object to an array, then returns the array. Mostly just for flattenR and uniqR

  • silenceUncaughtInPromise(promise: Promise<any>): Promise<any>
  • silentRejection(error: any): Promise<any>
  • 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[]): any[]
  • Reduce function which un-nests a single level of arrays

  • values(obj: Obj): any[]
  • Given an object, return its enumerable property values

Object literals

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

Generated using TypeDoc