UNPKG

abi.js

Version:

[![typescript-icon]][typescript-link] [![license-icon]][license-link] [![status-icon]][status-link] [![ci-icon]][ci-link] [![twitter-icon]][twitter-link]

183 lines (181 loc) 4.34 kB
"use strict"; 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/parser.ts var parser_exports = {}; __export(parser_exports, { parse_arg: () => parse_arg, parse_args: () => parse_args, parse_arr: () => parse_arr, parse_obj: () => parse_obj, parse_param: () => parse_param, parse_params: () => parse_params, parse_prop: () => parse_prop, parse_props: () => parse_props, parse_str: () => parse_str, parse_val: () => parse_val }); module.exports = __toCommonJS(parser_exports); function parse_val(val) { if (typeof val !== "string") { return val; } val = val.trim(); const lc_val = val.toLowerCase(); if (lc_val === "null") { return null; } if (lc_val === "undefined") { return void 0; } if (lc_val === "true") { return true; } if (lc_val === "false") { return false; } if (!Number.isNaN(Number(val))) { return Number(val); } if (/^('|")(.*?)\1$/gms.test(val)) { return parse_str(val); } if (/^\[(.*?)\]$/gms.test(val)) { return parse_arr(val); } if (/^\{(.*?)\}$/gms.test(val)) { return parse_obj(val); } return val; } function parse_str(val) { val = val.trim(); const res = /^('|")(.*?)\1$/gms.exec(val); if (res) { val = res[2]; } return val; } function parse_arr(val) { const res = /^\[(.*?)\]$/gms.exec(val); if (res) { val = res[1]; } const arr = []; const items = val.split(","); for (const i in items) { arr[i] = parse_val(items[i]); } return arr; } function parse_obj(val) { const res = /^\{(.*?)\}$/gms.exec(val); if (res) { val = res[1]; } const obj = {}; const items = val.split(","); for (const item of items) { const [key, val2] = parse_prop(item); obj[key] = val2; } return obj; } function parse_params(val) { const res = /^\((.*?)\)$/gms.exec(val); if (res) { val = res[1]; } const params = {}; const items = val.split(","); for (const item of items) { let [name, val2] = parse_param(item); if (val2 !== void 0) { val2 = parse_val(val2); } params[name] = val2; } return params; } function parse_args(val) { const res = /^\((.*?)\)$/gms.exec(val); if (res) { val = res[1]; } const args = {}; const items = val.split(","); for (const item of items) { const [key, val2] = parse_prop(item); args[key] = val2; } return args; } function parse_props(val) { const res = /^\{(.*?)\}$/gms.exec(val); if (res) { val = res[1]; } return parse_args(val); } function parse_prop(val) { if (/^([a-zA-Z0-9_\$]*)\s*\:\s*(.+)$/gms.test(val)) { return parse_arg(val); } return parse_param(val); } function parse_arg(val) { val = val.trim(); const res = /^([a-zA-Z0-9_\$]*)\s*\:\s*(.+)$/gms.exec(val); if (res) { const key = parse_val(res[1]); let value = parse_val(res[2]); if (value !== void 0) { value = parse_val(value); } if (typeof value === "string") { const [_, param] = parse_param(value); return [key, param ?? value]; } if (value === void 0) { return parse_param(key); } return [key, value]; } return parse_param(val); } function parse_param(val) { val = val.trim(); const res = /^([a-zA-Z0-9_\$]+[a-zA-Z0-9_\$]*)\s*\=\s*(.+)$/gms.exec(val); if (res) { return [parse_str(res[1]), parse_val(res[2])]; } return [parse_val(val)]; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { parse_arg, parse_args, parse_arr, parse_obj, parse_param, parse_params, parse_prop, parse_props, parse_str, parse_val });