@platform/state
Version:
A small, simple, strongly typed, [rx/observable] state-machine.
60 lines (59 loc) • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TreeIdentity = exports.key = exports.namespace = exports.stripNamespace = exports.hasNamespace = exports.parse = exports.format = void 0;
var common_1 = require("../common");
var util_value_1 = require("@platform/util.value");
var toString = function (input) { return (input || '').trim(); };
function format(namespace, key) {
return "".concat(toString(namespace), ":").concat(toString(key));
}
exports.format = format;
function parse(input) {
input = toString(input);
if (hasNamespace(input)) {
var key_1 = stripNamespace(input);
var namespace_1 = input.substring(0, input.length - key_1.length - 1);
var id_1 = "".concat(namespace_1, ":").concat(key_1);
return { namespace: namespace_1, key: key_1, id: id_1 };
}
else {
return { namespace: '', key: input, id: input };
}
}
exports.parse = parse;
function hasNamespace(input) {
input = toString(input);
return !input.startsWith(':') && input.includes(':');
}
exports.hasNamespace = hasNamespace;
function stripNamespace(input) {
input = toString(input).replace(/^:/, '');
if (hasNamespace(input)) {
var index = input.lastIndexOf(':');
return index > -1 ? input.substring(index + 1) : input;
}
else {
return input;
}
}
exports.stripNamespace = stripNamespace;
function namespace(input) {
return parse(input).namespace;
}
exports.namespace = namespace;
function key(input) {
return stripNamespace(input);
}
exports.key = key;
exports.TreeIdentity = {
toNodeId: common_1.toNodeId,
toString: toString,
format: format,
parse: parse,
stripNamespace: stripNamespace,
hasNamespace: hasNamespace,
namespace: namespace,
key: key,
cuid: function () { return util_value_1.id.cuid(); },
slug: function () { return util_value_1.id.shortid(); },
};