@knapsack/app
Version:
Build Design Systems on top of knapsack, by Basalt
146 lines (120 loc) • 4.08 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.validateSpec = validateSpec;
exports.hasItemsInItems = hasItemsInItems;
exports.uniqueArray = uniqueArray;
exports.flattenNestedArray = flattenNestedArray;
exports.flattenArray = flattenArray;
exports.isBase64 = isBase64;
exports.timer = timer;
var _schemaUtils = require("@knapsack/schema-utils");
var _schemaKsTemplateSpecSlots = _interopRequireDefault(require("../json-schemas/schemaKsTemplateSpecSlots"));
/**
* Copyright (C) 2018 Basalt
This file is part of Knapsack.
Knapsack is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
Knapsack is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along
with Knapsack; if not, see <https://www.gnu.org/licenses>.
*/
// IMPORTANT: Only put vanilla JavaScript in here: this should all work in the server or client
function validateSpec(spec) {
let ok = true;
const msgs = [];
if (spec === null || spec === void 0 ? void 0 : spec.props) {
const result = (0, _schemaUtils.validateSchema)(spec.props);
if (!result.ok) {
ok = false;
msgs.push('Invalid "spec.props":');
msgs.push(result.message);
}
}
if (spec === null || spec === void 0 ? void 0 : spec.slots) {
const result = (0, _schemaUtils.validateDataAgainstSchema)(_schemaKsTemplateSpecSlots.default, spec.slots);
if (!result.ok) {
ok = false;
msgs.push('Invalid "spec.slots":');
msgs.push(result.message);
result.errors.forEach(e => msgs.push(e.message));
}
}
return {
ok,
message: msgs.join('\n')
};
}
/**
* Is Some Of This Array In That Array?
* Are any of the items in arrayA in arrayB?
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function hasItemsInItems(arrayA, arrayB) {
return arrayA.some(a => arrayB.includes(a));
}
/**
* Make an array unique by removing duplicate entries.
* @param {Array} ar - Array to make unique
* @returns {Array} - A unique array
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function uniqueArray(ar) {
const j = {};
ar.forEach(v => {
j[`${v}::${typeof v}`] = v;
});
return Object.keys(j).map(v => j[v]);
}
/**
* Flatten nest array
* @param {Array} arr
* @return {Array}
* @link https://stackoverflow.com/a/15030117
*/
function flattenNestedArray(arr) {
return arr.reduce((flat, toFlatten) => flat.concat(Array.isArray(toFlatten) ? flattenNestedArray(toFlatten) : toFlatten), []);
}
function flattenArray(arrayOfArrays) {
return [].concat(...arrayOfArrays);
}
function isBase64(v, {
mimeRequired = false,
allowMime = true
} = {}) {
if (v instanceof Boolean || typeof v === 'boolean') {
return false;
} // if (!(opts instanceof Object)) {
// opts = {};
// }
//
// if (opts.allowEmpty === false && v === '') {
// return false;
// }
let regex = '(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+/]{3}=)?';
const mimeRegex = '(data:\\w+\\/[a-zA-Z\\+\\-\\.]+;base64,)';
if (mimeRequired === true) {
regex = mimeRegex + regex;
} else if (allowMime === true) {
regex = `${mimeRegex}?${regex}`;
} // if (opts.paddingRequired === false) {
// regex =
// '(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}(==)?|[A-Za-z0-9+\\/]{3}=?)?';
// }
const result = new RegExp(`^${regex}$`, 'gi').test(v);
return result;
}
function timer() {
const startTime = new Date().getTime();
return () => {
const endTime = new Date().getTime();
return (endTime - startTime) / 1000;
};
}