@villedemontreal/general-utils
Version:
General utilities library
163 lines • 8.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NOT_MISSING_VALUES = exports.NOT_EMPTY_VALUES = exports.EMPTY_VALUES = exports.EMPTY_PATTERN = exports.EMPTY_FUNCTION = exports.EMPTY_OBJECT = exports.EMPTY_ARRAY = exports.EMPTY_STRING = void 0;
const chai_1 = require("chai");
const _ = require("lodash");
const _1 = require(".");
const collectionUtils_1 = require("./collectionUtils");
exports.EMPTY_STRING = '';
exports.EMPTY_ARRAY = [];
Object.freeze(exports.EMPTY_ARRAY);
exports.EMPTY_OBJECT = {};
Object.freeze(exports.EMPTY_OBJECT);
exports.EMPTY_FUNCTION = _.noop;
Object.freeze(exports.EMPTY_FUNCTION);
exports.EMPTY_PATTERN = /^$/;
Object.freeze(exports.EMPTY_PATTERN);
const EMPTY_MAP = new Map();
Object.freeze(EMPTY_MAP);
const EMPTY_SET = new Set();
Object.freeze(EMPTY_SET);
exports.EMPTY_VALUES = [
undefined,
null,
exports.EMPTY_STRING,
exports.EMPTY_ARRAY,
exports.EMPTY_OBJECT,
EMPTY_MAP,
EMPTY_SET,
];
Object.freeze(exports.EMPTY_VALUES);
const EMPTY_VALUES_MAP = (0, collectionUtils_1.toDictionary)(exports.EMPTY_VALUES, _1.getValueDescriptionWithType);
Object.freeze(EMPTY_VALUES_MAP);
const NOT_EMPTY_MAP = new Map();
NOT_EMPTY_MAP.set(123, 'ABC');
Object.freeze(NOT_EMPTY_MAP);
const NOT_EMPTY_SET = new Set();
NOT_EMPTY_SET.add(123);
Object.freeze(NOT_EMPTY_SET);
exports.NOT_EMPTY_VALUES = [
true,
false,
NaN,
123,
Infinity,
'🍺',
exports.EMPTY_FUNCTION,
{ id: 1 },
['🍕'],
exports.EMPTY_PATTERN,
new Date(),
new Error(),
Number(123),
NOT_EMPTY_MAP,
NOT_EMPTY_SET,
];
Object.freeze(exports.NOT_EMPTY_VALUES);
const NOT_EMPTY_VALUES_MAP = (0, collectionUtils_1.toDictionary)(exports.NOT_EMPTY_VALUES, _1.getValueDescriptionWithType);
Object.freeze(NOT_EMPTY_VALUES_MAP);
exports.NOT_MISSING_VALUES = _.concat(_.reject(exports.EMPTY_VALUES, _.isNil), exports.NOT_EMPTY_VALUES);
Object.freeze(exports.NOT_MISSING_VALUES);
const NOT_MISSING_VALUES_MAP = (0, collectionUtils_1.toDictionary)(exports.NOT_MISSING_VALUES, _1.getValueDescriptionWithType);
Object.freeze(NOT_MISSING_VALUES_MAP);
describe('Collection Utility', () => {
describe('#isEmpty', () => {
exports.EMPTY_VALUES.forEach((value) => {
it(`should return \`true\` for ${(0, _1.getValueDescriptionWithType)(value)}`, () => {
chai_1.assert.isTrue((0, collectionUtils_1.isEmpty)(value));
});
});
exports.NOT_EMPTY_VALUES.forEach((value) => {
it(`should return \`false\` for ${(0, _1.getValueDescriptionWithType)(value)}`, () => {
chai_1.assert.isFalse((0, collectionUtils_1.isEmpty)(value));
});
});
});
describe('#isCollection', () => {
chai_1.assert.isTrue((0, collectionUtils_1.isCollection)(exports.EMPTY_OBJECT));
chai_1.assert.isTrue((0, collectionUtils_1.isCollection)({ id: 1 }));
chai_1.assert.isTrue((0, collectionUtils_1.isCollection)(exports.EMPTY_ARRAY));
chai_1.assert.isTrue((0, collectionUtils_1.isCollection)(['🍕']));
chai_1.assert.isTrue((0, collectionUtils_1.isCollection)(EMPTY_MAP));
chai_1.assert.isTrue((0, collectionUtils_1.isCollection)(NOT_EMPTY_MAP));
chai_1.assert.isTrue((0, collectionUtils_1.isCollection)(EMPTY_SET));
chai_1.assert.isTrue((0, collectionUtils_1.isCollection)(NOT_EMPTY_SET));
});
describe('#removeEmptyValues', () => {
it('should remove empty values from arrays', () => {
const values = _.concat([], exports.EMPTY_VALUES, exports.NOT_EMPTY_VALUES);
const result = (0, collectionUtils_1.removeEmptyValues)(values);
chai_1.assert.deepStrictEqual(result, exports.NOT_EMPTY_VALUES);
});
it('should remove empty values from objects', () => {
const values = _.merge({}, EMPTY_VALUES_MAP, NOT_EMPTY_VALUES_MAP);
const result = (0, collectionUtils_1.removeEmptyValues)(values);
chai_1.assert.deepStrictEqual(result, NOT_EMPTY_VALUES_MAP);
});
});
describe('#removeMissingValues', () => {
it('should remove missing values from arrays', () => {
const values = _.concat([], exports.EMPTY_VALUES, exports.NOT_EMPTY_VALUES);
const result = (0, collectionUtils_1.removeMissingValues)(values);
chai_1.assert.deepStrictEqual(result, exports.NOT_MISSING_VALUES);
});
it('should remove missing values from objects', () => {
const values = _.merge({}, EMPTY_VALUES_MAP, NOT_EMPTY_VALUES_MAP);
const result = (0, collectionUtils_1.removeMissingValues)(values);
chai_1.assert.deepStrictEqual(result, NOT_MISSING_VALUES_MAP);
});
});
describe('#filter', () => {
it('should be no-op if no filter is provided', () => {
const values = _.merge({}, EMPTY_VALUES_MAP, NOT_EMPTY_VALUES_MAP);
chai_1.assert.deepStrictEqual((0, collectionUtils_1.filter)(values, null), values);
});
});
describe('#getCartesianProduct', () => {
it('should return expected value', () => {
const valueSet1 = ['A', 1, true, null, exports.EMPTY_FUNCTION, exports.EMPTY_ARRAY];
const valueSet2 = [exports.EMPTY_PATTERN, NaN, false, undefined, exports.EMPTY_OBJECT];
const result = (0, collectionUtils_1.getCartesianProduct)(valueSet1, valueSet2);
// prettier-ignore
const expectedResult = [
['A', exports.EMPTY_PATTERN], ['A', NaN], ['A', false], ['A', undefined], ['A', exports.EMPTY_OBJECT],
[1, exports.EMPTY_PATTERN], [1, NaN], [1, false], [1, undefined], [1, exports.EMPTY_OBJECT],
[true, exports.EMPTY_PATTERN], [true, NaN], [true, false], [true, undefined], [true, exports.EMPTY_OBJECT],
[null, exports.EMPTY_PATTERN], [null, NaN], [null, false], [null, undefined], [null, exports.EMPTY_OBJECT],
[exports.EMPTY_FUNCTION, exports.EMPTY_PATTERN], [exports.EMPTY_FUNCTION, NaN], [exports.EMPTY_FUNCTION, false], [exports.EMPTY_FUNCTION, undefined], [exports.EMPTY_FUNCTION, exports.EMPTY_OBJECT],
[exports.EMPTY_ARRAY, exports.EMPTY_PATTERN], [exports.EMPTY_ARRAY, NaN], [exports.EMPTY_ARRAY, false], [exports.EMPTY_ARRAY, undefined], [exports.EMPTY_ARRAY, exports.EMPTY_OBJECT]
];
chai_1.assert.deepStrictEqual(result, expectedResult);
});
});
describe('Utility functions', () => {
describe('#isMatching', () => {
it('should behave properly', () => {
chai_1.assert.isTrue((0, collectionUtils_1.isMatching)({ A: 1, B: 2, C: 3 }, { A: 1, B: 2 }));
chai_1.assert.isFalse((0, collectionUtils_1.isMatching)({ A: 1, B: 2 }, { A: 1, B: 2, C: 3 }));
chai_1.assert.isTrue((0, collectionUtils_1.isMatching)({ A: 1, B: 2 }, { A: '???', B: 2, C: 3 }, ['B']));
});
});
describe('#isCompatible', () => {
it('should support fixed values', () => {
chai_1.assert.isTrue((0, collectionUtils_1.isCompatible)({ A: 1, B: 2, C: 3 }, { A: 1, B: 2 }));
chai_1.assert.isTrue((0, collectionUtils_1.isCompatible)({ A: 1, B: 2, C: 3 }, { A: 1, B: 2 }, ['A', 'B']));
chai_1.assert.isFalse((0, collectionUtils_1.isCompatible)({ A: 1, B: 2 }, { A: 1, B: 2, C: 3 }));
chai_1.assert.isFalse((0, collectionUtils_1.isCompatible)({ A: 1, B: 2 }, { A: '???', B: 2 }));
chai_1.assert.isTrue((0, collectionUtils_1.isCompatible)({ A: 1, B: 2 }, { A: 1, B: 2 }, ['C']));
chai_1.assert.isTrue((0, collectionUtils_1.isCompatible)({ A: 1, B: 2 }, { A: '???', B: 2, C: 3 }, ['B']));
chai_1.assert.isTrue((0, collectionUtils_1.isCompatible)({ A: 1, B: 2, C: 3 }, { A: '???', B: 2, C: 3 }, ['B', 'C']));
const rule = (value) => value < 3;
chai_1.assert.isFalse((0, collectionUtils_1.isCompatible)({ A: 1, B: 2, C: 3 }, { A: rule, B: rule, C: rule }));
chai_1.assert.isTrue((0, collectionUtils_1.isCompatible)({ A: 0, B: 1, C: 2 }, { A: rule, B: rule, C: rule }));
chai_1.assert.isTrue((0, collectionUtils_1.isCompatible)({ A: 999, B: 2, C: 999 }, { A: rule, B: rule, C: rule }, ['B']));
});
it('should support compatibility rules', () => {
chai_1.assert.isTrue((0, collectionUtils_1.isCompatible)({ A: 1, B: 2, C: 3 }, { A: _.isNumber, C: 3 }));
chai_1.assert.isFalse((0, collectionUtils_1.isCompatible)({ A: 1, B: 2, C: 3 }, { B: _.isBoolean }));
chai_1.assert.isTrue((0, collectionUtils_1.isCompatible)({ A: 1, B: 2, C: 3 }, { D: _.isUndefined }));
});
});
});
});
//# sourceMappingURL=collectionUtils.test.js.map