unbag
Version:
一个专门用来开发npm工具的包
58 lines • 1.67 kB
JavaScript
import { AbsolutePath, usePath } from "./path.mjs";
function isObject(value) {
return Object.prototype.toString.call(value) === "[object Object]";
}
var 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;
});
};
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;
};
const sleep = ms => new Promise(r => setTimeout(r, ms));
const useRoot = params => {
const {
finalUserConfig
} = params;
const path = usePath();
return new AbsolutePath({
content: path.resolve(finalUserConfig.root)
});
};
const unSafeFunctionWrapper = func => {
return func;
};
export { Locale, arraify, filterNullable, isObject, safeObj, sleep, unSafeFunctionWrapper, useRoot };