Create a reference path from either an array of PathSegments, or multiple PathSegment arguments
Parameters
firstArg(PathSegment | Array<PathSegment>)pathSegments...Any
Examples
import { createPath } from 'referencejs';
createPath(['foo', 'bar']);
createPath('foo', 'bar');
// Throws an error
createPath(['foo'], 'bar')
createPath({}, 9);- Throws Error if both an array of PathSegments and multiple PathSegment arguments are passed
- Throws Error if something besides a PathSegment is passed
Returns Path
Creates a Reference.
Parameters
firstArg(PathSegment | Array<PathSegment>)pathSegments...Any
Examples
import { createReference } from 'referencejs'
const store = {
foo: {
bar: 5
},
baz: ['hi']
}
// create a reference to 'foo.bar' in plain JS object
createReference('foo', 'bar');
createReference(['foo', 'bar']);
//create a reference to 'baz[0]'
createReference('baz', 0);
createReference(['baz', 0]);Returns Any
retrieves a value at a reference from a store
Parameters
Examples
import { createReference, dereference } from 'referencejs';
const user = {
name: "john"
};
const userReference = createReference('auth', 'users', 'user_1');
const store = {
auth: {
users: {
user_1: user
}
}
};
dereference(store, userReference) === user;Returns Any The value at reference or EmptyRefrence if value is not present
A Symbol returned when a Reference or ImmutableReference is not present in Store or ImmutableStore
Examples
import { createReference, dereference } from 'referencejs';
const store = {};
const reference = createReference('nothing', 'here');
dereference(store, reference) === EmptyReferenceimport { Map } from 'immutable';
import { createReference, dereference } from 'referencejs/immutable';
const store = Map();
const reference = createReference('nothing', 'here');
dereference(store, reference) === EmptyReferenceTests whether the given argument is a PathSegment
Parameters
maybePathSegmentany
Examples
// Also imports from 'referencejs/immutable'
import { isPathSegment } from 'referencejs';
isPathSegment('users') === true
isPathSegment(5) === true
isPathSegment({}) === falseReturns boolean
tests whether the argument is a Path
Parameters
maybePathAny
Examples
import { isPath } from 'referencejs';
isPath(['foo', 0]) === true;
isPath(['', -10]) === false;Returns Boolean
Tests whether the argument is a Reference.
Parameters
maybeReferenceAny
Examples
import { isReference, createReference } from 'referencejs';
isReference(createReference('foo')) === true;
isReference({}) === false;
isReference(null) === false;Returns Any
Returns a new Store with value at reference
Parameters
storeStorereferenceReference Reference where the value should be placedvalueany The value to place in the store
Examples
import { createReference, resolveReference, dereference } from 'referencejs';
const user = {
name: "john"
};
const userReference = createReference('auth', 'users', 'user_1');
let store = {};
store = resolveReference(store, userReference, user);
dereference(store, userReference) === user;Returns Any A new Store containing value at reference
Traverses val and dereferences every reference.
Parameters
storeStorevalThe object to scan. References are dereferenced, all ArrayLikeObjects are iteratated, all PlainObjects are traversed, and everything else is returned unmodified.
Examples
import { createReference, resolveReference, smartDereference } from 'referencejs';
function createUserReference(user) {
return createReference('users', user.id);
}
let store = {};
const jon = {
id: 'user_1',
name: 'jon'
};
const jonReference = createUserReference(jon);
store = resolveReference(store, jonReference, jon);
const james = {
id: 'user_2',
name: 'james'
};
const jamesReference = createUserReference(james);
store = resolveReference(store, jamesReference, james);
const sally = {
id: 'user_3',
name: 'sally',
};
const sallyReference = createUserReference(sally);
store = resolveReference(store, sallyReference, sally);
const relations = [
{
from: jonReference,
to: sallyReference,
type: "husband"
},{
from: sallyReference,
to: jonReference,
type: "wife"
},{
from: sallyReference,
to: jamesReference,
type: "daughter"
},{
from: jonReference,
to: jamesReference,
type: "son-in-law"
},{
from: jamesReference,
to: jonReference,
type: "father-in-law"
},
];
// 'from' and 'to' will be their respective user objects in the store
const dereferencedRelations = smartDereference(store, relations);Returns Any A new object with all references dereferenced
Test if Reference is set in Store.
Parameters
Examples
import { storehasReference, createReference } from 'referencejs';
const store = {
foo: 5,
};
const reference = createReference('foo');
const emptyRefrence = createReference('bar');
storehasReference(store, foo) === true;
storehasReference(store, emptyRefrence) === false;Returns Boolean
a non-empty string, or a non-negative integer
Examples
const pathSegmentString = 'hi';
const PathSegmentInt = 0;A non-empty array of strings and non-negative integers (indeces). They describe how to traverse a JS object to retrieve a value. You should always use createPath. This validates the Path, future-proofs your code, and lets you switch from Plain to Immutable by changing an import path.
Examples
const store = {
auth: {
users: [
{
name: 'Jon'
}
]
}
}
const path = ['auth', 'users', 0, 'name'];A wrapper around at Path. You should always use createReference. This validates the Reference, future-proofs your code, and lets you switch from Plain to Immutable by changing an import path.
Examples
const store = {
auth: {
users: [
{
name: 'Jon'
}
]
}
}
const reference = {
path: ['auth', 'users', 0, 'name']
}A plain JS object
Examples
const store = {
foo: [{
bar: 'hi'
}]
};