UNPKG

@platform/state

Version:

A small, simple, strongly typed, [rx/observable] state-machine.

51 lines (50 loc) 1.34 kB
import { toNodeId } from '../common'; import { id } from '@platform/util.value'; const toString = (input) => (input || '').trim(); export function format(namespace, key) { return `${toString(namespace)}:${toString(key)}`; } export function parse(input) { input = toString(input); if (hasNamespace(input)) { const key = stripNamespace(input); const namespace = input.substring(0, input.length - key.length - 1); const id = `${namespace}:${key}`; return { namespace, key, id }; } else { return { namespace: '', key: input, id: input }; } } export function hasNamespace(input) { input = toString(input); return !input.startsWith(':') && input.includes(':'); } export function stripNamespace(input) { input = toString(input).replace(/^\:/, ''); if (hasNamespace(input)) { const index = input.lastIndexOf(':'); return index > -1 ? input.substring(index + 1) : input; } else { return input; } } export function namespace(input) { return parse(input).namespace; } export function key(input) { return stripNamespace(input); } export const TreeIdentity = { toNodeId, toString, format, parse, stripNamespace, hasNamespace, namespace, key, cuid: () => id.cuid(), slug: () => id.shortid(), };