unbag
Version:
一个专门用来开发npm工具的包
72 lines (71 loc) • 2.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Locale = void 0;
exports.arraify = arraify;
exports.filterNullable = void 0;
exports.isObject = isObject;
exports.useRoot = exports.unSafeFunctionWrapper = exports.sleep = exports.safeObj = void 0;
var _path = require("./path.cjs");
function isObject(value) {
return Object.prototype.toString.call(value) === "[object Object]";
}
var Locale = exports.Locale = /* @__PURE__ */(Locale2 => {
Locale2["zh_cn"] = "zh_cn";
return Locale2;
})(Locale || {});
const filterNullable = (list, isNullable) => {
return list.filter(e => {
if (isNullable) {
return isNullable(e);
}
return !!e;
});
};
exports.filterNullable = filterNullable;
function arraify(target) {
return Array.isArray(target) ? target : [target];
}
const safeObj = (obj, name, config = {}) => {
const errorMsgFormat = config.errorMsgFormat || ((objName2, key) => {
return `${objName2} [${key}] undefined or null`;
});
const objName = name;
const p = new Proxy({}, {
get(_target, property, _receiver) {
return () => {
if (typeof property !== "string") {
throw new Error(`safely ${objName} property type must string`);
}
const key = property.slice(1);
const value = obj[key];
if (value === void 0 || value === null) {
throw new Error(errorMsgFormat(objName, key));
}
if (typeof value === "object") {
return safeObj(value, `${objName}.${key}`, config);
}
return value;
};
}
});
return p;
};
exports.safeObj = safeObj;
const sleep = ms => new Promise(r => setTimeout(r, ms));
exports.sleep = sleep;
const useRoot = params => {
const {
finalUserConfig
} = params;
const path = (0, _path.usePath)();
return new _path.AbsolutePath({
content: path.resolve(finalUserConfig.root)
});
};
exports.useRoot = useRoot;
const unSafeFunctionWrapper = func => {
return func;
};
exports.unSafeFunctionWrapper = unSafeFunctionWrapper;