@uirouter/core/common/common | @uirouter/angular
Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @uirouter/core/common/common

Random utility functions used in the UI-Router code

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

preferred

Index

Type aliases

IInjectable

IInjectable: Function | any[]

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) {

}];

Mapper

Mapper<X, T>: (x: X, key?: string | number) => T

Type parameters

  • X

  • T

Type declaration

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

      • x: X
      • Optional key: string | number

      Returns T

Predicate

Predicate<X>: (x?: X) => boolean

Type parameters

  • X

Type declaration

    • (x?: X): boolean
    • Parameters

      • Optional x: X

      Returns boolean

PredicateBinary

PredicateBinary<X, Y>: (x?: X, y?: Y) => boolean

Type parameters

  • X

  • Y

Type declaration

    • (x?: X, y?: Y): boolean
    • Parameters

      • Optional x: X
      • Optional y: Y

      Returns boolean

Variables

Const assertMap

assertMap: <T, U>(mapFn: (t: T) => U, errMsg: string | Function) => (t: T) => U = assertFn

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: (t: T) => U, errMsg: string | Function): (t: T) => U
    • Type parameters

      • T

      • U

      Parameters

      • mapFn: (t: T) => U
          • (t: T): U
          • Parameters

            • t: T

            Returns U

      • errMsg: string | Function

      Returns (t: T) => U

        • (t: T): U
        • Parameters

          • t: T

          Returns U

Const assertPredicate

assertPredicate: <T>(predicate: Predicate<T>, errMsg: string | Function) => Predicate<T> = assertFn

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

Const equals

equals: any = angular.equals || _equals

Const extend

extend: _extend = Object.assign || _extend

Const forEach

forEach: any = angular.forEach || _forEach

Const fromJson

fromJson: any = angular.fromJson || JSON.parse.bind(JSON)

Const inArray

inArray: typeof _inArray = curry(_inArray) as any

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

Let mapObj

mapObj: <T, U>(collection: {}, callback: Mapper<T, U>, target?: typeof collection) => {} = map

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

Type declaration

    • <T, U>(collection: {}, callback: Mapper<T, U>, target?: typeof collection): {}
    • Type parameters

      • T

      • U

      Parameters

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

      Returns {}

      • [key: string]: U

Const pushTo

pushTo: typeof _pushTo = curry(_pushTo) as any

pushes a values to an array and returns the value

Const removeFrom

removeFrom: typeof _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

Const root

root: any = (typeof self === 'object' && self.self === self && self) ||(typeof global === 'object' && global.global === global && global) ||this

Const toJson

toJson: any = angular.toJson || JSON.stringify.bind(JSON)

Functions

_extend

  • _extend(toObj: Obj, ...fromObjs: Obj[]): any

_inArray

  • _inArray(array: any[], obj: any): boolean
  • _inArray(array: any[]): (obj: any) => boolean

_pushTo

  • _pushTo<T>(arr: T[], val: T): T
  • _pushTo<T>(arr: T[]): (val: T) => T

_removeFrom

  • _removeFrom<T>(array: T[], obj: T): T[]
  • _removeFrom<T>(array: T[]): (obj: T) => T[]

Const allTrueR

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

    example
    
    let vals = [ 1, true, {}, "hello world"];
    vals.reduce(allTrueR, true); // true
    
    vals.push(0);
    vals.reduce(allTrueR, true); // false

    Parameters

    • memo: boolean
    • elem: any

    Returns any

ancestors

Const anyTrueR

  • anyTrueR(memo: boolean, elem: any): any
  • 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

  • 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

    • memo: TypedMap<any>
    • keyValTuple: any[]

    Returns TypedMap<any>

arrayTuples

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

    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

    • Rest ...args: any[]

    Returns any[]

assertFn

  • assertFn(predicateOrMap: Function, errMsg?: string | Function): any

copy

  • copy(src: Obj, dest?: Obj): any

createProxyFunctions

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

    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

    • Optional fnNames: string[]

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

    • Default value latebind: boolean = false

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

    Returns Obj

defaults

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

    Parameters

    • opts: any
    • Rest ...defaultsList: Obj[]

    Returns any

Const deregAll

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

    Parameters

    • functions: Function[]

    Returns void

filter

  • filter<T>(collection: T[], callback: (t: T, key?: number) => boolean): T[]
  • filter<T>(collection: TypedMap<T>, callback: (t: T, key?: string) => boolean): TypedMap<T>
  • 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: (t: T, key?: number) => boolean
        • (t: T, key?: number): boolean
        • Parameters

          • t: T
          • Optional key: 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: (t: T, key?: string) => boolean
        • (t: T, key?: string): boolean
        • Parameters

          • t: T
          • Optional key: string

          Returns boolean

    Returns TypedMap<T>

find

Const flatten

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

    example
    
    let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
    flatten(input) // [ "a", "b", "c", "d", "double, "nested" ]

    Parameters

    • arr: any[]

    Returns any

Const flattenR

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

    example
    
    let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
    input.reduce(unnestR, []) // [ "a", "b", "c", "d", "double, "nested" ]

    Parameters

    • memo: any[]
    • elem: any

    Returns any[]

identity

  • identity(x: any): any

Const inherit

  • 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

    Parameters

    • parent: Obj
    • Optional extra: Obj

    Returns any

map

  • map<T, U>(collection: T[], callback: Mapper<T, U>, target?: typeof collection): U[]
  • map<T, U>(collection: {}, callback: Mapper<T, U>, target?: typeof collection): {}
  • 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>
    • Optional target: typeof collection

    Returns U[]

  • Maps an array or object properties using a callback function

    Type parameters

    • T

    • U

    Parameters

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

    Returns {}

    • [key: string]: U

Const mergeR

  • mergeR(memo: Obj, item: Obj): any

noop

  • noop(): any

omit

  • omit(obj: Obj, propNames: string[]): 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: string[]

      an Array of strings, which are the blacklisted property names

    Returns Obj

Const pairs

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

    example
    
    pairs({ foo: "FOO", bar: "BAR }) // [ [ "foo", "FOO" ], [ "bar": "BAR" ] ]

    Parameters

    Returns any[][]

pick

  • pick(obj: Obj, propNames: string[]): 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: string[]

      an Array of strings, which are the whitelisted property names

    Returns Obj

pluck

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

    Returns {}

    • [key: string]: any

pushR

  • pushR(arr: any[], obj: any): any[]

Const silenceUncaughtInPromise

Const silentRejection

  • silentRejection(error: any): Promise<any>

tail

  • tail<T>(arr: T[]): T

Const uniqR

  • uniqR<T>(acc: T[], token: T): T[]

Const unnest

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

    example
    
    let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
    unnest(input) // [ "a", "b", "c", "d", [ "double, "nested" ] ]

    Parameters

    • arr: any[]

    Returns any

Const unnestR

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

    example
    
    let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
    input.reduce(unnestR, []) // [ "a", "b", "c", "d", [ "double, "nested" ] ]

    Parameters

    • memo: any[]
    • elem: any[]

    Returns any[]

Const values

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

    example
    
    let foo = { a: 1, b: 2, c: 3 }
    let vals = values(foo); // [ 1, 2, 3 ]

    Parameters

    Returns any[]

Generated using TypeDoc