@e22m4u/js-repository 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,23 +0,0 @@
1
- import {DataType} from './data-type.js';
2
- import {Service} from '@e22m4u/js-service';
3
-
4
- /**
5
- * Empty values definer.
6
- */
7
- export class EmptyValuesDefiner extends Service {
8
- /**
9
- * Set empty values of.
10
- *
11
- * @param dataType
12
- * @param emptyValues
13
- */
14
- setEmptyValuesOf(dataType: DataType, emptyValues: unknown[]): this;
15
-
16
- /**
17
- * Is empty.
18
- *
19
- * @param dataType
20
- * @param value
21
- */
22
- isEmpty(dataType: DataType, value: unknown): boolean;
23
- }
@@ -1,66 +0,0 @@
1
- import {DataType} from './data-type.js';
2
- import {Service} from '@e22m4u/js-service';
3
- import {isDeepEqual} from '../../../utils/index.js';
4
- import {InvalidArgumentError} from '../../../errors/index.js';
5
-
6
- /**
7
- * Empty values definer.
8
- */
9
- export class EmptyValuesDefiner extends Service {
10
- /**
11
- * Empty values map.
12
- *
13
- * @type {Map<string, *[]>}
14
- */
15
- _emptyValuesMap = new Map([
16
- [DataType.ANY, [undefined, null]],
17
- [DataType.STRING, [undefined, null, '']],
18
- [DataType.NUMBER, [undefined, null, 0]],
19
- [DataType.BOOLEAN, [undefined, null]],
20
- [DataType.ARRAY, [undefined, null, []]],
21
- [DataType.OBJECT, [undefined, null, {}]],
22
- ]);
23
-
24
- /**
25
- * Set empty values of data type.
26
- *
27
- * @param {string} dataType
28
- * @param {*[]} emptyValues
29
- * @returns {EmptyValuesDefiner}
30
- */
31
- setEmptyValuesOf(dataType, emptyValues) {
32
- if (!Object.values(DataType).includes(dataType))
33
- throw new InvalidArgumentError(
34
- 'The argument "dataType" of the EmptyValuesDefiner.setEmptyValuesOf ' +
35
- 'must be one of data types: %l, but %v given.',
36
- Object.values(DataType),
37
- dataType,
38
- );
39
- if (!Array.isArray(emptyValues))
40
- throw new InvalidArgumentError(
41
- 'The argument "emptyValues" of the EmptyValuesDefiner.setEmptyValuesOf ' +
42
- 'must be an Array, but %v given.',
43
- emptyValues,
44
- );
45
- this._emptyValuesMap.set(dataType, emptyValues);
46
- return this;
47
- }
48
-
49
- /**
50
- * Is empty.
51
- *
52
- * @param {string} dataType
53
- * @param {*} value
54
- * @returns {boolean}
55
- */
56
- isEmpty(dataType, value) {
57
- if (!Object.values(DataType).includes(dataType))
58
- throw new InvalidArgumentError(
59
- 'The argument "dataType" of the EmptyValuesDefiner.isEmpty ' +
60
- 'must be one of data types: %l, but %v given.',
61
- Object.values(DataType),
62
- dataType,
63
- );
64
- return this._emptyValuesMap.get(dataType).some(v => isDeepEqual(v, value));
65
- }
66
- }
@@ -1,96 +0,0 @@
1
- import {expect} from 'chai';
2
- import {DataType} from './data-type.js';
3
- import {format} from '@e22m4u/js-format';
4
- import {Schema} from '../../../schema.js';
5
- import {EmptyValuesDefiner} from './empty-values-definer.js';
6
-
7
- const getEmptyValues = (definer, dataType) => {
8
- return definer['_emptyValuesMap'].get(dataType);
9
- };
10
-
11
- describe('EmptyValuesDefiner', function () {
12
- describe('_emptyValuesMap', function () {
13
- it('has default values', function () {
14
- const schema = new Schema();
15
- const S = schema.getService(EmptyValuesDefiner);
16
- expect(Array.from(S['_emptyValuesMap'])).to.be.eql([
17
- [DataType.ANY, [undefined, null]],
18
- [DataType.STRING, [undefined, null, '']],
19
- [DataType.NUMBER, [undefined, null, 0]],
20
- [DataType.BOOLEAN, [undefined, null]],
21
- [DataType.ARRAY, [undefined, null, []]],
22
- [DataType.OBJECT, [undefined, null, {}]],
23
- ]);
24
- });
25
- });
26
-
27
- describe('setEmptyValuesOf', function () {
28
- it('requires the parameter "dataType" to be a DataType enum', function () {
29
- const schema = new Schema();
30
- const S = schema.getService(EmptyValuesDefiner);
31
- const throwable = v => () => S.setEmptyValuesOf(v, []);
32
- const error = v =>
33
- format(
34
- 'The argument "dataType" of the EmptyValuesDefiner.setEmptyValuesOf ' +
35
- 'must be one of data types: %l, but %s given.',
36
- Object.values(DataType),
37
- v,
38
- );
39
- expect(throwable('str')).to.throw(error('"str"'));
40
- expect(throwable('')).to.throw(error('""'));
41
- expect(throwable(10)).to.throw(error('10'));
42
- expect(throwable(0)).to.throw(error('0'));
43
- expect(throwable(true)).to.throw(error('true'));
44
- expect(throwable(false)).to.throw(error('false'));
45
- expect(throwable(undefined)).to.throw(error('undefined'));
46
- expect(throwable(null)).to.throw(error('null'));
47
- expect(throwable({})).to.throw(error('Object'));
48
- expect(throwable([])).to.throw(error('Array'));
49
- throwable(DataType.ANY)();
50
- });
51
-
52
- it('requires the parameter "emptyValues" to be an Array', function () {
53
- const schema = new Schema();
54
- const S = schema.getService(EmptyValuesDefiner);
55
- const throwable = v => () => S.setEmptyValuesOf(DataType.ANY, v);
56
- const error = v =>
57
- format(
58
- 'The argument "emptyValues" of the EmptyValuesDefiner.setEmptyValuesOf ' +
59
- 'must be an Array, but %s given.',
60
- v,
61
- );
62
- expect(throwable('str')).to.throw(error('"str"'));
63
- expect(throwable('')).to.throw(error('""'));
64
- expect(throwable(10)).to.throw(error('10'));
65
- expect(throwable(0)).to.throw(error('0'));
66
- expect(throwable(true)).to.throw(error('true'));
67
- expect(throwable(false)).to.throw(error('false'));
68
- expect(throwable(undefined)).to.throw(error('undefined'));
69
- expect(throwable(null)).to.throw(error('null'));
70
- expect(throwable({})).to.throw(error('Object'));
71
- throwable([])();
72
- throwable([1, 2, 3])();
73
- });
74
-
75
- it('overrides default values of the given data type', function () {
76
- const schema = new Schema();
77
- const S = schema.getService(EmptyValuesDefiner);
78
- expect(getEmptyValues(S, DataType.ANY)).eql([undefined, null]);
79
- S.setEmptyValuesOf(DataType.ANY, [1, 2, 3]);
80
- expect(getEmptyValues(S, DataType.ANY)).eql([1, 2, 3]);
81
- });
82
- });
83
-
84
- describe('isEmpty', function () {
85
- it('returns true if the given value exists in the given type', function () {
86
- const schema = new Schema();
87
- const S = schema.getService(EmptyValuesDefiner);
88
- S.setEmptyValuesOf(DataType.ANY, []);
89
- expect(S.isEmpty(DataType.ANY, 'foo')).to.be.false;
90
- S.setEmptyValuesOf(DataType.ANY, ['bar']);
91
- expect(S.isEmpty(DataType.ANY, 'foo')).to.be.false;
92
- S.setEmptyValuesOf(DataType.ANY, ['bar', 'foo']);
93
- expect(S.isEmpty(DataType.ANY, 'foo')).to.be.true;
94
- });
95
- });
96
- });