@yopem/object-parser
Version:
142 lines (140 loc) • 4.01 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
ObjectParser: () => ObjectParser
});
module.exports = __toCommonJS(index_exports);
var ObjectParser = class _ObjectParser {
value;
constructor(value) {
this.value = value;
}
has(...path) {
if (path.length < 1) {
throw new TypeError("Invalid path");
}
let value = this.value;
for (const key of path) {
if (typeof value !== "object" || value === null) {
return false;
}
if (!(key in value)) {
return false;
}
value = value[key];
}
return true;
}
get(...path) {
if (path.length < 1) {
throw new TypeError("Invalid path");
}
let value = this.value;
for (let i = 0; i < path.length; i++) {
if (typeof value !== "object" || value === null) {
throw new Error(
`Value in path ${path.slice(0, i + 1).join(".")} is not an object`
);
}
if (!(path[i] in value)) {
throw new Error(`Path ${path.slice(0, i + 1).join(".")} does not exist`);
}
value = value[path[i]];
}
return value;
}
isString(...path) {
return typeof this.get(...path) === "string";
}
getString(...path) {
const value = this.get(...path);
if (typeof value !== "string") {
throw new Error(`Value in path ${path.join(".")} is not a string`);
}
return value;
}
isNumber(...path) {
return typeof this.get(...path) === "number";
}
getNumber(...path) {
const value = this.get(...path);
if (typeof value !== "number") {
throw new Error(`Value in path ${path.join(".")} is not a number`);
}
return value;
}
isBoolean(...path) {
return typeof this.get(...path) === "boolean";
}
getBoolean(...path) {
const value = this.get(...path);
if (typeof value !== "boolean") {
throw new Error(`Value in path ${path.join(".")} is not a boolean`);
}
return value;
}
isBigInt(...path) {
return typeof this.get(...path) === "bigint";
}
getBigInt(...path) {
const value = this.get(...path);
if (typeof value !== "bigint") {
throw new Error(`Value in path ${path.join(".")} is not a bigint`);
}
return value;
}
isObject(...path) {
const value = this.get(...path);
return typeof value === "object" && value !== null;
}
getObject(...path) {
const value = this.get(...path);
if (typeof value !== "object" || value === null) {
throw new Error(`Value in path ${path.join(".")} is not an object`);
}
return value;
}
isArray(...path) {
return Array.isArray(this.get(...path));
}
getArray(...path) {
const value = this.get(...path);
if (!Array.isArray(value)) {
throw new Error(`Value in path ${path.join(".")} is not an array`);
}
return value;
}
isNull(...path) {
const value = this.get(...path);
return value === null;
}
isUndefined(...path) {
const value = this.get(...path);
return value === void 0;
}
createParser(...path) {
return new _ObjectParser(this.getObject(...path));
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ObjectParser
});