@e22m4u/js-repository 0.1.7 → 0.1.8

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.
package/README.md CHANGED
@@ -314,6 +314,9 @@ schema.defineModel({
314
314
  нужно производить с входящими данными.
315
315
 
316
316
  - `trim` удаление пробельных символов с начала и конца строки
317
+ - `toUpperCase` перевод строки в верхний регистр
318
+ - `toLowerCase` перевод строки в нижний регистр
319
+ - `toTitleCase` перевод строки в регистр заголовка
317
320
 
318
321
  **Пример**
319
322
 
@@ -330,7 +333,8 @@ schema.defineModel({
330
333
  name: {
331
334
  type: DataType.STRING,
332
335
  transform: [ // трансформеры свойства "name"
333
- 'trim', // обрезать пробелы в начале и в конце строки
336
+ 'trim', // удалить пробелы в начале и конце строки
337
+ 'toTitleCase', // перевод в регистр заголовка
334
338
  ],
335
339
  },
336
340
  },
package/docs/index.html CHANGED
@@ -150,6 +150,9 @@
150
150
  нужно производить с входящими данными.</p>
151
151
  <ul>
152
152
  <li><code>trim</code> удаление пробельных символов с начала и конца строки</li>
153
+ <li><code>toUpperCase</code> перевод строки в верхний регистр</li>
154
+ <li><code>toLowerCase</code> перевод строки в нижний регистр</li>
155
+ <li><code>toTitleCase</code> перевод строки в регистр заголовка</li>
153
156
  </ul>
154
157
  <p><strong>Пример</strong></p>
155
158
  <p>Трансформеры указываются в объявлении свойства модели параметром
@@ -157,7 +160,7 @@
157
160
  указать несколько названий, то используется массив. Если трансформер
158
161
  имеет настройки, то используется объект, где ключом является название
159
162
  трансформера, а значением его параметры.</p>
160
- <pre><code class="language-js"><span class="hl-4">schema</span><span class="hl-1">.</span><span class="hl-0">defineModel</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-4">name:</span><span class="hl-1"> </span><span class="hl-2">&#39;user&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">properties:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">name:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">type:</span><span class="hl-1"> </span><span class="hl-4">DataType</span><span class="hl-1">.</span><span class="hl-7">STRING</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">transform:</span><span class="hl-1"> [ </span><span class="hl-5">// трансформеры свойства &quot;name&quot;</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;trim&#39;</span><span class="hl-1">, </span><span class="hl-5">// обрезать пробелы в начале и в конце строки</span><br/><span class="hl-1"> ],</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> },</span><br/><span class="hl-1">});</span>
163
+ <pre><code class="language-js"><span class="hl-4">schema</span><span class="hl-1">.</span><span class="hl-0">defineModel</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-4">name:</span><span class="hl-1"> </span><span class="hl-2">&#39;user&#39;</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">properties:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">name:</span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-4">type:</span><span class="hl-1"> </span><span class="hl-4">DataType</span><span class="hl-1">.</span><span class="hl-7">STRING</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-4">transform:</span><span class="hl-1"> [ </span><span class="hl-5">// трансформеры свойства &quot;name&quot;</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;trim&#39;</span><span class="hl-1">, </span><span class="hl-5">// удалить пробелы в начале и конце строки</span><br/><span class="hl-1"> </span><span class="hl-2">&#39;toTitleCase&#39;</span><span class="hl-1">, </span><span class="hl-5">// перевод в регистр заголовка</span><br/><span class="hl-1"> ],</span><br/><span class="hl-1"> },</span><br/><span class="hl-1"> },</span><br/><span class="hl-1">});</span>
161
164
  </code><button>Copy</button></pre>
162
165
  <a id="md:репозиторий" class="tsd-anchor"></a><h2><a href="#md:репозиторий">Репозиторий</a></h2><p>Выполняет операции чтения и записи документов определенной модели.
163
166
  Получить репозиторий можно методом <code>getRepository</code> экземпляра схемы.</p>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/js-repository",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Модуль для работы с базами данных для Node.js",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -1 +1,4 @@
1
1
  export * from './trim-transformer.js';
2
+ export * from './to-lower-case-transformer.js';
3
+ export * from './to-upper-case-transformer.js';
4
+ export * from './to-title-case-transformer.js';
@@ -1 +1,4 @@
1
1
  export * from './trim-transformer.js';
2
+ export * from './to-lower-case-transformer.js';
3
+ export * from './to-upper-case-transformer.js';
4
+ export * from './to-title-case-transformer.js';
@@ -0,0 +1,6 @@
1
+ import {PropertyTransformer} from '../property-transformer.js';
2
+
3
+ /**
4
+ * To lower case transformer.
5
+ */
6
+ export declare type toLowerCaseTransformer = PropertyTransformer;
@@ -0,0 +1,19 @@
1
+ import {InvalidArgumentError} from '../../../../../errors/index.js';
2
+
3
+ /**
4
+ * To lower case transformer.
5
+ *
6
+ * @param {*} value
7
+ * @param {undefined} options
8
+ * @param {object} context
9
+ * @returns {string|undefined|null}
10
+ */
11
+ export function toLowerCaseTransformer(value, options, context) {
12
+ if (value == null) return value;
13
+ if (typeof value === 'string') return value.toLowerCase();
14
+ throw new InvalidArgumentError(
15
+ 'The property transformer %v requires a String value, but %v given.',
16
+ context.transformerName,
17
+ value,
18
+ );
19
+ }
@@ -0,0 +1,39 @@
1
+ import {expect} from 'chai';
2
+ import {format} from '@e22m4u/js-format';
3
+ import {toLowerCaseTransformer} from './to-lower-case-transformer.js';
4
+
5
+ describe('toLowerCaseTransformer', function () {
6
+ it('returns undefined and null values as is', function () {
7
+ const res1 = toLowerCaseTransformer(undefined, undefined, {});
8
+ const res2 = toLowerCaseTransformer(null, undefined, {});
9
+ expect(res1).to.be.undefined;
10
+ expect(res2).to.be.null;
11
+ });
12
+
13
+ it('converts the given string to lower case', function () {
14
+ const res = toLowerCaseTransformer('TEST', undefined, {});
15
+ expect(res).to.be.eq('test');
16
+ });
17
+
18
+ it('throws an error if the given value is not a string', function () {
19
+ const throwable = v => () =>
20
+ toLowerCaseTransformer(v, undefined, {
21
+ transformerName: 'toLowerCase',
22
+ });
23
+ const error = v =>
24
+ format(
25
+ 'The property transformer "toLowerCase" requires a String value, but %s given.',
26
+ v,
27
+ );
28
+ expect(throwable(10)).to.throw(error('10'));
29
+ expect(throwable(0)).to.throw(error('0'));
30
+ expect(throwable(true)).to.throw(error('true'));
31
+ expect(throwable(false)).to.throw(error('false'));
32
+ expect(throwable({})).to.throw(error('Object'));
33
+ expect(throwable([])).to.throw(error('Array'));
34
+ throwable('str')();
35
+ throwable('')();
36
+ throwable(undefined)();
37
+ throwable(null)();
38
+ });
39
+ });
@@ -0,0 +1,6 @@
1
+ import {PropertyTransformer} from '../property-transformer.js';
2
+
3
+ /**
4
+ * To title case transformer.
5
+ */
6
+ export declare type toTitleCaseTransformer = PropertyTransformer;
@@ -0,0 +1,22 @@
1
+ import {InvalidArgumentError} from '../../../../../errors/index.js';
2
+
3
+ /**
4
+ * To title case transformer.
5
+ *
6
+ * @param {*} value
7
+ * @param {undefined} options
8
+ * @param {object} context
9
+ * @returns {string|undefined|null}
10
+ */
11
+ export function toTitleCaseTransformer(value, options, context) {
12
+ if (value == null) return value;
13
+ if (typeof value === 'string')
14
+ return value.replace(/\p{L}\S*/gu, text => {
15
+ return text.charAt(0).toUpperCase() + text.substring(1).toLowerCase();
16
+ });
17
+ throw new InvalidArgumentError(
18
+ 'The property transformer %v requires a String value, but %v given.',
19
+ context.transformerName,
20
+ value,
21
+ );
22
+ }
@@ -0,0 +1,41 @@
1
+ import {expect} from 'chai';
2
+ import {format} from '@e22m4u/js-format';
3
+ import {toTitleCaseTransformer} from './to-title-case-transformer.js';
4
+
5
+ describe('toTitleCaseTransformer', function () {
6
+ it('returns undefined and null values as is', function () {
7
+ const res1 = toTitleCaseTransformer(undefined, undefined, {});
8
+ const res2 = toTitleCaseTransformer(null, undefined, {});
9
+ expect(res1).to.be.undefined;
10
+ expect(res2).to.be.null;
11
+ });
12
+
13
+ it('converts the given string to title case', function () {
14
+ const res1 = toTitleCaseTransformer('TEST', undefined, {});
15
+ const res2 = toTitleCaseTransformer('test', undefined, {});
16
+ expect(res1).to.be.eq('Test');
17
+ expect(res2).to.be.eq('Test');
18
+ });
19
+
20
+ it('throws an error if the given value is not a string', function () {
21
+ const throwable = v => () =>
22
+ toTitleCaseTransformer(v, undefined, {
23
+ transformerName: 'toTitleCase',
24
+ });
25
+ const error = v =>
26
+ format(
27
+ 'The property transformer "toTitleCase" requires a String value, but %s given.',
28
+ v,
29
+ );
30
+ expect(throwable(10)).to.throw(error('10'));
31
+ expect(throwable(0)).to.throw(error('0'));
32
+ expect(throwable(true)).to.throw(error('true'));
33
+ expect(throwable(false)).to.throw(error('false'));
34
+ expect(throwable({})).to.throw(error('Object'));
35
+ expect(throwable([])).to.throw(error('Array'));
36
+ throwable('str')();
37
+ throwable('')();
38
+ throwable(undefined)();
39
+ throwable(null)();
40
+ });
41
+ });
@@ -0,0 +1,6 @@
1
+ import {PropertyTransformer} from '../property-transformer.js';
2
+
3
+ /**
4
+ * To upper case transformer.
5
+ */
6
+ export declare type toUpperCaseTransformer = PropertyTransformer;
@@ -0,0 +1,19 @@
1
+ import {InvalidArgumentError} from '../../../../../errors/index.js';
2
+
3
+ /**
4
+ * To upper case transformer.
5
+ *
6
+ * @param {*} value
7
+ * @param {undefined} options
8
+ * @param {object} context
9
+ * @returns {string|undefined|null}
10
+ */
11
+ export function toUpperCaseTransformer(value, options, context) {
12
+ if (value == null) return value;
13
+ if (typeof value === 'string') return value.toUpperCase();
14
+ throw new InvalidArgumentError(
15
+ 'The property transformer %v requires a String value, but %v given.',
16
+ context.transformerName,
17
+ value,
18
+ );
19
+ }
@@ -0,0 +1,39 @@
1
+ import {expect} from 'chai';
2
+ import {format} from '@e22m4u/js-format';
3
+ import {toUpperCaseTransformer} from './to-upper-case-transformer.js';
4
+
5
+ describe('toUpperCaseTransformer', function () {
6
+ it('returns undefined and null values as is', function () {
7
+ const res1 = toUpperCaseTransformer(undefined, undefined, {});
8
+ const res2 = toUpperCaseTransformer(null, undefined, {});
9
+ expect(res1).to.be.undefined;
10
+ expect(res2).to.be.null;
11
+ });
12
+
13
+ it('converts the given string to upper case', function () {
14
+ const res = toUpperCaseTransformer('test', undefined, {});
15
+ expect(res).to.be.eq('TEST');
16
+ });
17
+
18
+ it('throws an error if the given value is not a string', function () {
19
+ const throwable = v => () =>
20
+ toUpperCaseTransformer(v, undefined, {
21
+ transformerName: 'toUpperCase',
22
+ });
23
+ const error = v =>
24
+ format(
25
+ 'The property transformer "toUpperCase" requires a String value, but %s given.',
26
+ v,
27
+ );
28
+ expect(throwable(10)).to.throw(error('10'));
29
+ expect(throwable(0)).to.throw(error('0'));
30
+ expect(throwable(true)).to.throw(error('true'));
31
+ expect(throwable(false)).to.throw(error('false'));
32
+ expect(throwable({})).to.throw(error('Object'));
33
+ expect(throwable([])).to.throw(error('Array'));
34
+ throwable('str')();
35
+ throwable('')();
36
+ throwable(undefined)();
37
+ throwable(null)();
38
+ });
39
+ });
@@ -1,5 +1,8 @@
1
1
  import {Service} from '@e22m4u/js-service';
2
2
  import {trimTransformer} from './builtin/index.js';
3
+ import {toUpperCaseTransformer} from './builtin/index.js';
4
+ import {toLowerCaseTransformer} from './builtin/index.js';
5
+ import {toTitleCaseTransformer} from './builtin/index.js';
3
6
  import {InvalidArgumentError} from '../../../../errors/index.js';
4
7
 
5
8
  /**
@@ -13,6 +16,9 @@ export class PropertyTransformerRegistry extends Service {
13
16
  */
14
17
  _transformers = {
15
18
  trim: trimTransformer,
19
+ toUpperCase: toUpperCaseTransformer,
20
+ toLowerCase: toLowerCaseTransformer,
21
+ toTitleCase: toTitleCaseTransformer,
16
22
  };
17
23
 
18
24
  /**
@@ -1,6 +1,9 @@
1
1
  import {expect} from 'chai';
2
2
  import {format} from '@e22m4u/js-format';
3
3
  import {trimTransformer} from './builtin/index.js';
4
+ import {toUpperCaseTransformer} from './builtin/index.js';
5
+ import {toLowerCaseTransformer} from './builtin/index.js';
6
+ import {toTitleCaseTransformer} from './builtin/index.js';
4
7
  import {PropertyTransformerRegistry} from './property-transformer-registry.js';
5
8
 
6
9
  describe('PropertyTransformerRegistry', function () {
@@ -9,6 +12,9 @@ describe('PropertyTransformerRegistry', function () {
9
12
  const S = new PropertyTransformerRegistry();
10
13
  expect(S['_transformers']).to.be.eql({
11
14
  trim: trimTransformer,
15
+ toUpperCase: toUpperCaseTransformer,
16
+ toLowerCase: toLowerCaseTransformer,
17
+ toTitleCase: toTitleCaseTransformer,
12
18
  });
13
19
  });
14
20