@rashedmakkouk/dev-utils
Version:
Utility library.
44 lines (43 loc) • 1.5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/** Utilities */
const isArray_1 = __importDefault(require("lodash/isArray"));
const isNumber_1 = __importDefault(require("lodash/isNumber"));
const isPlainObject_1 = __importDefault(require("lodash/isPlainObject"));
const isString_1 = __importDefault(require("lodash/isString"));
const trim_whitespace_1 = __importDefault(require("./trim-whitespace"));
/**
* Converts any value to array.
*
* Useful for multi value fields as 'group_concat'.
*
* @returns New array based on supplied options.
*/
function toArray(value, options = {}) {
const { separator, parseNumber } = options;
if (value == null || value === '') {
return [];
}
if ((0, isString_1.default)(value)) {
const trimmedValue = (0, trim_whitespace_1.default)(value);
const nextValue = trimmedValue
.split(separator ?? ',')
.filter((n) => !!n);
return !parseNumber ? nextValue : nextValue.map(Number);
}
else if ((0, isArray_1.default)(value)) {
return !parseNumber
? [...value]
: value.map((n) => Number(n) ?? n);
}
else if ((0, isNumber_1.default)(value) || (0, isPlainObject_1.default)(value)) {
return [value];
}
else {
return [];
}
}
exports.default = toArray;