yewtils
Version:
239 lines (229 loc) • 6.25 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
const cacheStringFunction = (fn) => {
const cache = /* @__PURE__ */ Object.create(null);
return (str) => {
const hit = cache[str];
return hit || (cache[str] = fn(str));
};
};
function createStringConverter(fn) {
return cacheStringFunction((value) => {
const inputArray = value.match(/[A-Z\xC0-\xD6\xD8-\xDE]?[a-z\xDF-\xF6\xF8-\xFF]+|[A-Z\xC0-\xD6\xD8-\xDE]+(?![a-z\xDF-\xF6\xF8-\xFF])|\d+/g);
if (inputArray === null || !inputArray.length)
return value;
return inputArray.reduce(fn, "");
});
}
function toKebabCase(value) {
return createStringConverter((acc, cur, i) => {
cur = cur.toLowerCase();
if (i !== 0)
cur = `-${cur}`;
return acc += cur;
})(value);
}
function toCamelCase(value) {
return createStringConverter((acc, cur, i) => {
cur = cur.toLowerCase();
if (i !== 0)
cur = cur.substring(0, 1).toUpperCase() + cur.substring(1);
return acc += cur;
})(value);
}
function toPascalCase(value) {
return createStringConverter((acc, cur) => {
cur = cur.toLowerCase();
cur = cur.substring(0, 1).toUpperCase() + cur.substring(1);
return acc += cur;
})(value);
}
const createHash = cacheStringFunction((str) => {
let hash = 5381;
let i = str.length;
while (i)
hash = hash * 33 ^ str.charCodeAt(--i);
return `${(hash >> 0).toString(16)}`;
});
const defineProperty = (obj, key, value) => {
Object.defineProperty(obj, key, {
configurable: true,
enumerable: false,
value
});
};
const hasOwnProperty = (val, key) => Object.prototype.hasOwnProperty.call(val, key);
const extend = Object.assign;
const toTypeString = (value) => Object.prototype.toString.call(value);
const toNumber = (val) => {
const n = parseFloat(val);
return isNaN(n) ? val : n;
};
const toRawType = (value) => {
return toTypeString(value).slice(8, -1);
};
function guard(_value, isMatched) {
return isMatched;
}
function notNullish(v) {
return v != null;
}
function noNull(v) {
return v !== null;
}
function notUndefined(v) {
return v !== void 0;
}
function isTruthy(v) {
return Boolean(v);
}
function assert(condition, message) {
if (!condition)
throw new Error(message);
}
const isArray = Array.isArray;
const isMap = (val) => toTypeString(val) === "[object Map]";
const isSet = (val) => toTypeString(val) === "[object Set]";
const isDate = (val) => val instanceof Date;
const isFunction = (val) => typeof val === "function";
const isString = (val) => typeof val === "string";
const isSymbol = (val) => typeof val === "symbol";
const isObject = (val) => val !== null && typeof val === "object";
const isPromise = (val) => {
return isObject(val) && isFunction(val.then) && isFunction(val.catch);
};
const isPlainObject = (val) => toTypeString(val) === "[object Object]";
const isIntegerKey = (key) => isString(key) && key !== "NaN" && key[0] !== "-" && `${parseInt(key, 10)}` === key;
const EMPTY_OBJ = Object.freeze({});
function emptyObject() {
return EMPTY_OBJ;
}
const EMPTY_ARR = Object.freeze([]);
function emptyArray() {
return EMPTY_ARR;
}
const NO_OP = () => {
};
const NO = () => false;
const remove = (arr, el) => {
const i = arr.indexOf(el);
if (i > -1)
arr.splice(i, 1);
};
const hasChanged = (value, oldValue) => !Object.is(value, oldValue);
const invokeArrayFns = (fns, ...arg) => {
let args = [];
if (arg !== void 0) {
if (!isArray(arg))
args.push(arg);
else
args = [...arg];
}
fns.forEach((fn) => {
fn(...args);
});
};
function last(array) {
return at(array, -1);
}
function at(array, index) {
if (array.at)
return array.at(index);
const len = array.length;
if (!len)
return void 0;
if (index < 0)
index += len;
return array[index];
}
function range(...args) {
let start, stop, step;
if (args.length === 1) {
start = 0;
step = 1;
[stop] = args;
} else {
[start, stop, step = 1] = args;
}
const arr = [];
let current = start;
while (current < stop) {
arr.push(current);
current += step || 1;
}
return arr;
}
function move(arr, from, to) {
arr.splice(to, 0, arr.splice(from, 1)[0]);
return arr;
}
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
function sleep(ms, callback) {
return new Promise(
(resolve) => setTimeout(() => __async(this, null, function* () {
yield callback == null ? void 0 : callback();
resolve();
}), ms)
);
}
exports.EMPTY_ARR = EMPTY_ARR;
exports.EMPTY_OBJ = EMPTY_OBJ;
exports.NO = NO;
exports.NO_OP = NO_OP;
exports.assert = assert;
exports.at = at;
exports.cacheStringFunction = cacheStringFunction;
exports.createHash = createHash;
exports.defineProperty = defineProperty;
exports.emptyArray = emptyArray;
exports.emptyObject = emptyObject;
exports.extend = extend;
exports.guard = guard;
exports.hasChanged = hasChanged;
exports.hasOwnProperty = hasOwnProperty;
exports.invokeArrayFns = invokeArrayFns;
exports.isArray = isArray;
exports.isDate = isDate;
exports.isFunction = isFunction;
exports.isIntegerKey = isIntegerKey;
exports.isMap = isMap;
exports.isObject = isObject;
exports.isPlainObject = isPlainObject;
exports.isPromise = isPromise;
exports.isSet = isSet;
exports.isString = isString;
exports.isSymbol = isSymbol;
exports.isTruthy = isTruthy;
exports.last = last;
exports.move = move;
exports.noNull = noNull;
exports.notNullish = notNullish;
exports.notUndefined = notUndefined;
exports.range = range;
exports.remove = remove;
exports.sleep = sleep;
exports.toCamelCase = toCamelCase;
exports.toKebabCase = toKebabCase;
exports.toNumber = toNumber;
exports.toPascalCase = toPascalCase;
exports.toRawType = toRawType;
exports.toTypeString = toTypeString;
;