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.
139 lines (138 loc) • 5.3 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/utils.ts
var utils_exports = {};
__export(utils_exports, {
define: () => define,
findSafeBreakPoint: () => findSafeBreakPoint,
getGraphemes: () => getGraphemes,
getSegments: () => getSegments,
isArrayLike: () => isArrayLike,
isNumber: () => import_is_number.default,
isObject: () => isObject,
isSafeKey: () => isSafeKey,
isValid: () => isValid,
isValidObject: () => isValidObject,
size: () => size,
toKey: () => toKey,
unquote: () => unquote
});
module.exports = __toCommonJS(utils_exports);
var import_is_number = __toESM(require("is-number"));
var import_intl_segmenter = require("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 ((0, import_is_number.default)(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 import_intl_segmenter.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");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
define,
findSafeBreakPoint,
getGraphemes,
getSegments,
isArrayLike,
isNumber,
isObject,
isSafeKey,
isValid,
isValidObject,
size,
toKey,
unquote
});
//# sourceMappingURL=utils.js.map