tweak-tools
Version:
Tweak your React projects until awesomeness
48 lines (47 loc) • 1.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalize = exports.format = exports.sanitize = exports.schema = void 0;
const v8n_1 = __importDefault(require("v8n"));
// the options attribute is either an key value object or an array
const schema = (_o, s) => (0, v8n_1.default)()
.schema({
options: (0, v8n_1.default)().passesAnyOf((0, v8n_1.default)().object(), (0, v8n_1.default)().array()),
})
.test(s);
exports.schema = schema;
const sanitize = (value, { values }) => {
if (values.indexOf(value) < 0)
throw Error(`Selected value doesn't match Select options`);
return value;
};
exports.sanitize = sanitize;
const format = (value, { values }) => {
return values.indexOf(value);
};
exports.format = format;
const normalize = (input) => {
let { value, options } = input;
let keys;
let values;
if (Array.isArray(options)) {
values = options;
keys = options.map((o) => String(o));
}
else {
values = Object.values(options);
keys = Object.keys(options);
}
if (!('value' in input))
value = values[0];
else if (!values.includes(value)) {
keys.unshift(String(value));
values.unshift(value);
}
if (!Object.values(options).includes(value))
options[String(value)] = value;
return { value, settings: { keys, values } };
};
exports.normalize = normalize;