@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
112 lines (111 loc) • 3.02 kB
JavaScript
//#region node_modules/unist-util-is/lib/index.js
/**
* Generate an assertion from a test.
*
* Useful if you’re going to test many nodes, for example when creating a
* utility where something else passes a compatible test.
*
* The created function is a bit faster because it expects valid input only:
* a `node`, `index`, and `parent`.
*
* @param {Test} test
* * when nullish, checks if `node` is a `Node`.
* * when `string`, works like passing `(node) => node.type === test`.
* * when `function` checks if function passed the node is true.
* * when `object`, checks that all keys in test are in node, and that they have (strictly) equal values.
* * when `array`, checks if any one of the subtests pass.
* @returns {Check}
* An assertion.
*/
const convert = (function(test) {
if (test === null || test === void 0) return ok;
if (typeof test === "function") return castFactory(test);
if (typeof test === "object") return Array.isArray(test) ? anyFactory(test) : propertiesFactory(test);
if (typeof test === "string") return typeFactory(test);
throw new Error("Expected function, string, or object as test");
});
/**
* @param {Array<Props | TestFunction | string>} tests
* @returns {Check}
*/
function anyFactory(tests) {
/** @type {Array<Check>} */
const checks = [];
let index = -1;
while (++index < tests.length) checks[index] = convert(tests[index]);
return castFactory(any);
/**
* @this {unknown}
* @type {TestFunction}
*/
function any(...parameters) {
let index$1 = -1;
while (++index$1 < checks.length) if (checks[index$1].apply(this, parameters)) return true;
return false;
}
}
/**
* Turn an object into a test for a node with a certain fields.
*
* @param {Props} check
* @returns {Check}
*/
function propertiesFactory(check) {
const checkAsRecord = check;
return castFactory(all);
/**
* @param {Node} node
* @returns {boolean}
*/
function all(node) {
const nodeAsRecord = node;
/** @type {string} */
let key;
for (key in check) if (nodeAsRecord[key] !== checkAsRecord[key]) return false;
return true;
}
}
/**
* Turn a string into a test for a node with a certain type.
*
* @param {string} check
* @returns {Check}
*/
function typeFactory(check) {
return castFactory(type);
/**
* @param {Node} node
*/
function type(node) {
return node && node.type === check;
}
}
/**
* Turn a custom test into a test for a node that passes that test.
*
* @param {TestFunction} testFunction
* @returns {Check}
*/
function castFactory(testFunction) {
return check;
/**
* @this {unknown}
* @type {Check}
*/
function check(value, index, parent) {
return Boolean(looksLikeANode(value) && testFunction.call(this, value, typeof index === "number" ? index : void 0, parent || void 0));
}
}
function ok() {
return true;
}
/**
* @param {unknown} value
* @returns {value is Node}
*/
function looksLikeANode(value) {
return value !== null && typeof value === "object" && "type" in value;
}
//#endregion
export { convert };
//# sourceMappingURL=index.mjs.map