expand-value
Version:
Get deeply nested values from an object, like dot-prop and get-value, but with support for advanced features like bracket-notation and more.
94 lines (93 loc) • 3.28 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/utils.ts
import isNumber from "is-number";
import { Segmenter } from "intl-segmenter";
var { defineProperty } = Reflect;
var isObject = /* @__PURE__ */ __name((val) => val && typeof val === "object" && !Array.isArray(val), "isObject");
var unquote = /* @__PURE__ */ __name((str) => {
if (!str) return "";
return str.replace(/^['"`]|['"`]$/g, "");
}, "unquote");
var define = /* @__PURE__ */ __name((node, key, value) => {
defineProperty(node, key, {
configurable: true,
enumerable: false,
writable: true,
value
});
}, "define");
var size = /* @__PURE__ */ __name((value) => {
if (value == null) return 0;
if (isNumber(value)) return String(value).length;
if (isObject(value)) return Object.keys(value).length;
if (typeof value.length === "number") return value.length;
if (typeof value.size === "number") return value.size;
return null;
}, "size");
var isValidObject = /* @__PURE__ */ __name((val) => {
return isObject(val) || Array.isArray(val) || typeof val === "function";
}, "isValidObject");
var isSafeKey = /* @__PURE__ */ __name((key) => {
return key !== "__proto__" && key !== "constructor" && key !== "prototype";
}, "isSafeKey");
var isValid = /* @__PURE__ */ __name((key, data, options) => {
if (!isSafeKey(key)) {
return false;
}
if (typeof options.isValid === "function") {
return options.isValid(key, data);
}
return true;
}, "isValid");
var isArrayLike = /* @__PURE__ */ __name((obj) => {
return obj != null && typeof obj === "object" && typeof obj.length === "number";
}, "isArrayLike");
var toKey = /* @__PURE__ */ __name((value) => {
if (typeof value === "symbol") return value;
if (value === 0 && 1 / value === -Infinity) return "-0";
return String(value);
}, "toKey");
var findSafeBreakPoint = /* @__PURE__ */ __name((input) => {
for (let i = input.length - 1; i >= 0; i--) {
if (/\s/.test(input[i]) || /^[\x20-\x7E]$/.test(input[i])) {
return i + 1;
}
}
return input.length;
}, "findSafeBreakPoint");
var getSegments = /* @__PURE__ */ __name((input, language = "en", granularity) => {
const segmenter = new Segmenter(language, { granularity, localeMatcher: "best fit" });
return Array.from(segmenter.segment(input)).map((segment) => segment.segment);
}, "getSegments");
var getGraphemes = /* @__PURE__ */ __name((input, language = "en", maxChunkLength = 500) => {
const graphemes = [];
let position = 0;
while (position < input.length) {
const remainingText = input.slice(position);
const chunkSize = Math.min(maxChunkLength, remainingText.length);
const potentialChunk = remainingText.slice(0, chunkSize);
const breakPoint = findSafeBreakPoint(potentialChunk);
const chunk = potentialChunk.slice(0, breakPoint);
const chunkSegments = getSegments(chunk, language, "grapheme");
graphemes.push(...chunkSegments);
position += breakPoint;
}
return graphemes;
}, "getGraphemes");
export {
define,
findSafeBreakPoint,
getGraphemes,
getSegments,
isArrayLike,
isNumber,
isObject,
isSafeKey,
isValid,
isValidObject,
size,
toKey,
unquote
};
//# sourceMappingURL=utils.mjs.map