UNPKG

mihawk

Version:

A tiny & simple mock server tool, support json,js,cjs,ts(typescript).

116 lines (115 loc) 4.05 kB
'use strict'; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isJsonStr = exports.isEmptyList = exports.isEmptyArr = exports.isEmptyObj = exports.isObjStrict = exports.isNaNStrict = exports.isNumStrict = exports.isSimpleJsonPropType = exports.isPrimitvieType = exports.isUndefinedOrNull = exports.isNullOrUndefined = exports.isNil = exports.isType = exports.getType = void 0; const JSON5 = __importStar(require("json5")); function getType(obj) { return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase(); } exports.getType = getType; function isType(obj, typeName) { return getType(obj) === (typeName === null || typeName === void 0 ? void 0 : typeName.toLowerCase()); } exports.isType = isType; function isNil(value) { return value === null || value === undefined; } exports.isNil = isNil; exports.isNullOrUndefined = isNil; exports.isUndefinedOrNull = isNil; function isPrimitvieType(obj) { const type = getType(obj); return ['bigint', 'boolean', 'null', 'number', 'string', 'symbol', 'undefined'].includes(type); } exports.isPrimitvieType = isPrimitvieType; function isSimpleJsonPropType(obj) { const type = getType(obj); if (['null', 'string', 'boolean', 'number'].includes(type)) { return true; } return false; } exports.isSimpleJsonPropType = isSimpleJsonPropType; function isNumStrict(n) { return !isNaNStrict(n) && isType(n, 'number'); } exports.isNumStrict = isNumStrict; function isNaNStrict(obj) { return obj !== obj; } exports.isNaNStrict = isNaNStrict; function isObjStrict(obj) { if (isNil(obj)) return false; if (typeof obj === 'object') { if (Array.isArray(obj)) { return false; } else if (obj instanceof RegExp || obj instanceof Date || obj instanceof Error) { return false; } else if (obj instanceof Map || obj instanceof Set || obj instanceof WeakMap || obj instanceof WeakSet) { return false; } else if (obj instanceof Uint8Array || obj instanceof Uint16Array || obj instanceof Uint32Array) { return false; } else { return isType(obj, 'object'); } } return false; } exports.isObjStrict = isObjStrict; function isEmptyObj(obj) { var _a; if (isObjStrict(obj)) { return !((_a = Object.keys(obj)) === null || _a === void 0 ? void 0 : _a.length); } return false; } exports.isEmptyObj = isEmptyObj; function isEmptyArr(arr) { if (Array.isArray(arr)) { return !arr.length; } return false; } exports.isEmptyArr = isEmptyArr; exports.isEmptyList = isEmptyArr; function isJsonStr(jsonStr, isJson5 = false) { let isValidJsonCnt = false; try { isValidJsonCnt = JSON.parse(jsonStr); if (!isValidJsonCnt && isJson5) { isValidJsonCnt = JSON5.parse(jsonStr); } } catch (e) { isValidJsonCnt = false; } return isValidJsonCnt; } exports.isJsonStr = isJsonStr;