@smartsoft001/utils 2.76.0 → 2.80.0

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.
Files changed (53) hide show
  1. package/package.json +4 -3
  2. package/src/index.js +5 -0
  3. package/src/index.js.map +1 -0
  4. package/src/lib/services/array/array.service.d.ts +23 -0
  5. package/src/lib/services/array/array.service.js +40 -0
  6. package/src/lib/services/array/array.service.js.map +1 -0
  7. package/src/lib/services/guid/guid.service.d.ts +6 -0
  8. package/src/lib/services/guid/guid.service.js +14 -0
  9. package/src/lib/services/guid/guid.service.js.map +1 -0
  10. package/src/lib/services/index.js +12 -0
  11. package/src/lib/services/index.js.map +1 -0
  12. package/src/lib/services/nip/nip.service.d.ts +14 -0
  13. package/src/lib/services/nip/nip.service.js +36 -0
  14. package/src/lib/services/nip/nip.service.js.map +1 -0
  15. package/src/lib/services/object/object.service.d.ts +15 -0
  16. package/src/lib/services/object/object.service.js +57 -0
  17. package/src/lib/services/object/object.service.js.map +1 -0
  18. package/src/lib/services/password/password.service.d.ts +14 -0
  19. package/src/lib/services/password/password.service.js +30 -0
  20. package/src/lib/services/password/password.service.js.map +1 -0
  21. package/src/lib/services/pesel/pesel.service.d.ts +14 -0
  22. package/src/lib/services/pesel/pesel.service.js +49 -0
  23. package/src/lib/services/pesel/pesel.service.js.map +1 -0
  24. package/src/lib/services/specification/specification.service.d.ts +33 -0
  25. package/src/lib/services/specification/specification.service.js +61 -0
  26. package/src/lib/services/specification/specification.service.js.map +1 -0
  27. package/src/lib/services/zip-code/zip-code.service.d.ts +14 -0
  28. package/src/lib/services/zip-code/zip-code.service.js +24 -0
  29. package/src/lib/services/zip-code/zip-code.service.js.map +1 -0
  30. package/.eslintrc.json +0 -24
  31. package/jest.config.ts +0 -11
  32. package/project.json +0 -37
  33. package/src/lib/services/array/array.service.spec.ts +0 -56
  34. package/src/lib/services/array/array.service.ts +0 -40
  35. package/src/lib/services/guid/guid.service.spec.ts +0 -10
  36. package/src/lib/services/guid/guid.service.ts +0 -10
  37. package/src/lib/services/nip/nip.service.spec.ts +0 -31
  38. package/src/lib/services/nip/nip.service.ts +0 -34
  39. package/src/lib/services/object/object.service.spec.ts +0 -97
  40. package/src/lib/services/object/object.service.ts +0 -55
  41. package/src/lib/services/password/password.service.spec.ts +0 -18
  42. package/src/lib/services/password/password.service.ts +0 -25
  43. package/src/lib/services/pesel/pesel.service.spec.ts +0 -31
  44. package/src/lib/services/pesel/pesel.service.ts +0 -48
  45. package/src/lib/services/specification/specification.service.spec.ts +0 -67
  46. package/src/lib/services/specification/specification.service.ts +0 -85
  47. package/src/lib/services/zip-code/zip-code.service.spec.ts +0 -31
  48. package/src/lib/services/zip-code/zip-code.service.ts +0 -20
  49. package/tsconfig.json +0 -22
  50. package/tsconfig.lib.json +0 -10
  51. package/tsconfig.spec.json +0 -14
  52. /package/src/{index.ts → index.d.ts} +0 -0
  53. /package/src/lib/services/{index.ts → index.d.ts} +0 -0
@@ -1,56 +0,0 @@
1
- import { ArrayService } from './array.service';
2
-
3
- describe('shared-utils: ArrayService', () => {
4
- describe('addItem()', () => {
5
- it('should add element to array', () => {
6
- const array = [1];
7
-
8
- ArrayService.addItem(array, 2);
9
-
10
- expect(array[1]).toBe(2);
11
- });
12
-
13
- it('should create new array', () => {
14
- const array = [1];
15
-
16
- const newArray = ArrayService.addItem(array, 2);
17
-
18
- expect(array).not.toBe(newArray);
19
- expect(array[0]).toBe(newArray[0]);
20
- expect(array[1]).toBe(newArray[1]);
21
- });
22
- });
23
-
24
- describe('removeItem()', () => {
25
- it('should remove element from array', () => {
26
- const array = [1, 2];
27
-
28
- ArrayService.removeItem(array, 2);
29
-
30
- expect(array.length).toBe(1);
31
- expect(!!array[1]).not.toBeTruthy();
32
- });
33
-
34
- it('should create new array', () => {
35
- const array = [1, 2];
36
-
37
- const newArray = ArrayService.removeItem(array, 2);
38
-
39
- expect(array).not.toBe(newArray);
40
- expect(array[0]).toBe(newArray[0]);
41
- expect(!!array[1]).toBe(!!newArray[1]);
42
- });
43
-
44
- describe('sort()', () => {
45
- it('should return sorted array', () => {
46
- const array = [{ key: 2 }, { key: 1 }, { key: -1 }];
47
-
48
- const result = ArrayService.sort(array, (item) => item.key);
49
-
50
- expect(array[2]).toBe(result[0]);
51
- expect(array[1]).toBe(result[1]);
52
- expect(array[0]).toBe(result[2]);
53
- });
54
- });
55
- });
56
- });
@@ -1,40 +0,0 @@
1
- import * as _ from 'lodash';
2
-
3
- export class ArrayService {
4
- /**
5
- * pushing item to input array and creating new array with same items
6
- * @param array {array} - input array
7
- * @param item {object} - new item
8
- * @returns {array} - new array with item
9
- */
10
- static addItem<T>(array: Array<T>, item: T): Array<T> {
11
- array.push(item);
12
-
13
- return [...array];
14
- }
15
-
16
- /**
17
- * removing item from input array and creating new array without item
18
- * @param array {array} - input array
19
- * @param item {object} - item to remove
20
- * @returns {array} - new array without item
21
- */
22
- static removeItem<T>(array: Array<T>, item: T): Array<T> {
23
- const index = array.indexOf(item);
24
- if (index > -1) {
25
- array.splice(index, 1);
26
- }
27
-
28
- return [...array];
29
- }
30
-
31
- /**
32
- * sort array
33
- * @param array {array} - input array
34
- * @param by {function} - function to get sort key
35
- * @returns {array} - sorted array
36
- */
37
- static sort<T>(array: Array<T>, by: (item: T) => unknown): Array<T> {
38
- return _.sortBy(array, by);
39
- }
40
- }
@@ -1,10 +0,0 @@
1
- import { Guid } from 'guid-typescript';
2
-
3
- import { GuidService } from './guid.service';
4
-
5
- describe('shared-utils: GuidService', () => {
6
- it('should create correct guid', () => {
7
- const guid = GuidService.create();
8
- expect(Guid.isGuid(guid)).toBeTruthy();
9
- });
10
- });
@@ -1,10 +0,0 @@
1
- import { Guid } from 'guid-typescript';
2
-
3
- export class GuidService {
4
- /***
5
- * Create guid as string
6
- */
7
- static create(): string {
8
- return Guid.raw();
9
- }
10
- }
@@ -1,31 +0,0 @@
1
- import { NipService } from './nip.service';
2
-
3
- describe('shared-utils: NipService', () => {
4
- describe('isValid()', () => {
5
- it('should return true when correct nip', () => {
6
- const result = NipService.isValid('5372527048');
7
-
8
- expect(result).toBeTruthy();
9
- });
10
-
11
- it('should return false when incorrect nip', () => {
12
- const result = NipService.isValid('5372527041');
13
-
14
- expect(result).not.toBeTruthy();
15
- });
16
- });
17
-
18
- describe('isInvalid()', () => {
19
- it('should return false when correct nip', () => {
20
- const result = NipService.isInvalid('5372527048');
21
-
22
- expect(result).not.toBeTruthy();
23
- });
24
-
25
- it('should return true when incorrect nip', () => {
26
- const result = NipService.isInvalid('5372527041');
27
-
28
- expect(result).toBeTruthy();
29
- });
30
- });
31
- });
@@ -1,34 +0,0 @@
1
- export class NipService {
2
- /***
3
- * Check valid nip format
4
- * @param nip {string} - nip string
5
- * @return return true when correct
6
- */
7
- static isValid(nip: string): boolean {
8
- if (typeof nip !== 'string') return false;
9
-
10
- //eslint-disable-next-line
11
- nip = nip.replace(/[\ \-]/gi, '');
12
-
13
- const weight = [6, 5, 7, 2, 3, 4, 5, 6, 7];
14
- let sum = 0;
15
- // tslint:disable-next-line:radix
16
- const controlNumber = parseInt(nip.substring(9, 10));
17
- const weightCount = weight.length;
18
- for (let i = 0; i < weightCount; i++) {
19
- // tslint:disable-next-line:radix
20
- sum += parseInt(nip.substr(i, 1)) * weight[i];
21
- }
22
-
23
- return sum % 11 === controlNumber;
24
- }
25
-
26
- /***
27
- * Check invalid nip format
28
- * @param nip {string} - nip string
29
- * @return return true when incorrect
30
- */
31
- static isInvalid(nip: string): boolean {
32
- return !NipService.isValid(nip);
33
- }
34
- }
@@ -1,97 +0,0 @@
1
- import { ObjectService } from './object.service';
2
-
3
- describe('shared-utils: ObjectService', () => {
4
- describe('createByType()', () => {
5
- it('should return same object when exist same type', () => {
6
- class Test {
7
- test1?: string;
8
- }
9
- const instance = new Test();
10
-
11
- const result = ObjectService.createByType(instance, Test);
12
-
13
- expect(result).toBe(instance);
14
- });
15
-
16
- it('should return other object when is other type', () => {
17
- class Test {
18
- test1?: string;
19
- }
20
- class Test2 {
21
- test1?: string;
22
- }
23
- const instance = new Test();
24
-
25
- const result = ObjectService.createByType(instance, Test2);
26
-
27
- expect(result).not.toBe(instance);
28
- });
29
-
30
- it('should return specific type when set object', () => {
31
- class Test {
32
- test1?: string;
33
- }
34
- const data = {
35
- test1: 'test123',
36
- };
37
-
38
- const result: Test = ObjectService.createByType(data, Test);
39
-
40
- expect(result.constructor).toBe(Test);
41
- expect(result.test1).toBe(data.test1);
42
- });
43
- });
44
-
45
- describe('removeTypes()', () => {
46
- it('should remove type from object', function () {
47
- class Test {
48
- test1?: string;
49
- }
50
- const obj = new Test();
51
-
52
- const result = ObjectService.removeTypes(obj);
53
-
54
- expect(result.constructor).not.toBe(Test);
55
- });
56
-
57
- it('should set correct data', function () {
58
- class Test {
59
- test1?: string;
60
- }
61
- const obj = new Test();
62
- obj.test1 = 'test123';
63
-
64
- const result = ObjectService.removeTypes(obj);
65
-
66
- expect(result.test1).toBe(obj.test1);
67
- });
68
-
69
- it('should remove only object types in parent', function () {
70
- class Test {
71
- test1?: string;
72
- date?: Date;
73
- }
74
- const obj = new Test();
75
- obj.test1 = 'test123';
76
- obj.date = new Date();
77
-
78
- const result = ObjectService.removeTypes(obj);
79
-
80
- expect(result.date instanceof Date).toBeTruthy();
81
- });
82
-
83
- it('should handle null fields', function () {
84
- class Test {
85
- test1?: string;
86
- date?: Date;
87
- }
88
- const obj = new Test();
89
- obj.test1 = undefined;
90
- obj.date = new Date();
91
-
92
- ObjectService.removeTypes(obj);
93
-
94
- expect(obj.test1 === undefined).toBeTruthy();
95
- });
96
- });
97
- });
@@ -1,55 +0,0 @@
1
- import { stringify } from 'flatted';
2
-
3
- export class ObjectService {
4
- /***
5
- * Create object with data
6
- * @param data {object} - data to set
7
- * @param type {type} - new type
8
- * @return - new type object
9
- */
10
- static createByType<T>(data: any, type: any): T {
11
- if (!data) return data as T;
12
-
13
- try {
14
- if (data instanceof type) return data as T;
15
- } catch (e) {
16
- console.warn(e);
17
- }
18
-
19
- const result = new type();
20
-
21
- Object.keys(data).forEach((key) => {
22
- result[key] = data[key];
23
- });
24
-
25
- return result;
26
- }
27
-
28
- /***
29
- * Remove object type from data
30
- * @param obj {object} - object
31
- * @return - object without type
32
- */
33
- static removeTypes(obj: any): any {
34
- if (!obj) return obj;
35
-
36
- const result = {} as any;
37
-
38
- Object.keys(obj).forEach((key) => {
39
- if (obj[key] && obj[key].constructor && !(obj[key] instanceof Date)) {
40
- let stringValue = '';
41
- try {
42
- stringValue = JSON.stringify(obj[key]);
43
- } catch (e) {
44
- console.warn("can't stringify without circular package");
45
- stringValue = stringify(obj[key]);
46
- }
47
- result[key] = JSON.parse(stringValue);
48
- } else {
49
- result[key] = obj[key];
50
- }
51
- });
52
-
53
- return result;
54
- }
55
- }
@@ -1,18 +0,0 @@
1
- import { PasswordService } from './password.service';
2
-
3
- describe('shared-utils: PasswordService', () => {
4
- it('should hash', async () => {
5
- const result = await PasswordService.hash('test');
6
-
7
- expect(result).toBeDefined();
8
- });
9
-
10
- it('should compare', async () => {
11
- const password = 'test';
12
- const hash = await PasswordService.hash(password);
13
-
14
- const result = await PasswordService.compare(password, hash);
15
-
16
- expect(result).toBeTruthy();
17
- });
18
- });
@@ -1,25 +0,0 @@
1
- import * as md5_ from 'md5';
2
-
3
- const md5 = md5_;
4
-
5
- // @dynamic
6
- export class PasswordService {
7
- /**
8
- * Hash password text
9
- * @param p {string} - text
10
- * @return - hashed text
11
- */
12
- static hash(p: string): Promise<string> {
13
- return Promise.resolve(md5(p));
14
- }
15
-
16
- /**
17
- * Compare password text with hashed text
18
- * @param p {string} - password text
19
- * @param h {string} - hashed text
20
- */
21
- static async compare(p: string, h: string): Promise<boolean> {
22
- const hp = await PasswordService.hash(p);
23
- return hp === h;
24
- }
25
- }
@@ -1,31 +0,0 @@
1
- import { PeselService } from './pesel.service';
2
-
3
- describe('shared-utils: PeselService', () => {
4
- describe('isValid()', () => {
5
- it('should return true when correct pesel', () => {
6
- const result = PeselService.isValid('91012008616');
7
-
8
- expect(result).toBeTruthy();
9
- });
10
-
11
- it('should return false when incorrect pesel', () => {
12
- const result = PeselService.isValid('91012008612');
13
-
14
- expect(result).not.toBeTruthy();
15
- });
16
- });
17
-
18
- describe('isInvalid()', () => {
19
- it('should return false when correct pesel', () => {
20
- const result = PeselService.isInvalid('91012008616');
21
-
22
- expect(result).not.toBeTruthy();
23
- });
24
-
25
- it('should return true when incorrect pesel', () => {
26
- const result = PeselService.isInvalid('91012008612');
27
-
28
- expect(result).toBeTruthy();
29
- });
30
- });
31
- });
@@ -1,48 +0,0 @@
1
- export class PeselService {
2
- /***
3
- * Check valid pesel format
4
- * @param pesel {string} - pesel string
5
- * @return return true when correct
6
- */
7
- static isValid(pesel: string): boolean {
8
- const reg = /^[0-9]{11}$/;
9
- if (reg.test(pesel) === false) return false;
10
- else {
11
- const digits = ('' + pesel).split('');
12
- // tslint:disable-next-line:radix
13
- if (
14
- parseInt(pesel.substring(4, 6)) > 31 ||
15
- parseInt(pesel.substring(2, 4)) > 12
16
- )
17
- return false;
18
-
19
- // tslint:disable-next-line:radix
20
- let checksum =
21
- (1 * parseInt(digits[0]) +
22
- 3 * parseInt(digits[1]) +
23
- 7 * parseInt(digits[2]) +
24
- 9 * parseInt(digits[3]) +
25
- 1 * parseInt(digits[4]) +
26
- 3 * parseInt(digits[5]) +
27
- 7 * parseInt(digits[6]) +
28
- 9 * parseInt(digits[7]) +
29
- 1 * parseInt(digits[8]) +
30
- 3 * parseInt(digits[9])) %
31
- 10;
32
- if (checksum === 0) checksum = 10;
33
- checksum = 10 - checksum;
34
-
35
- // tslint:disable-next-line:radix
36
- return parseInt(digits[10]) === checksum;
37
- }
38
- }
39
-
40
- /***
41
- * Check invalid pesel format
42
- * @param pesel {string} - pesel string
43
- * @return return true when incorrect
44
- */
45
- static isInvalid(pesel: string): boolean {
46
- return !PeselService.isValid(pesel);
47
- }
48
- }
@@ -1,67 +0,0 @@
1
- import { SpecificationService } from './specification.service';
2
-
3
- describe('shared-utils: SpecificationService', () => {
4
- describe('valid()', () => {
5
- it('should valid value', () => {
6
- const criteria = { a: 'test' };
7
- const value = { a: 'test' };
8
-
9
- const result = SpecificationService.valid(value, { criteria });
10
-
11
- expect(result).toBeTruthy();
12
- });
13
-
14
- it('should invalid value', () => {
15
- const criteria = { a: 'test' };
16
- const value = { a: 'test2' };
17
-
18
- const result = SpecificationService.valid(value, { criteria });
19
-
20
- expect(result).not.toBeTruthy();
21
- });
22
-
23
- it('should valid array value', () => {
24
- const criteria = { a: 'test1' };
25
- const value = { a: ['test1', 'test2'] };
26
-
27
- const result = SpecificationService.valid(value, { criteria });
28
-
29
- expect(result).toBeTruthy();
30
- });
31
-
32
- it('should invalid array value', () => {
33
- const criteria = { a: 'test1' };
34
- const value = { a: ['test2', 'test3'] };
35
-
36
- const result = SpecificationService.valid(value, { criteria });
37
-
38
- expect(result).not.toBeTruthy();
39
- });
40
- });
41
-
42
- describe('getSqlCriteria()', () => {
43
- it('should return string query for string criteria', () => {
44
- const criteria = { a: 'test' };
45
-
46
- const result = SpecificationService.getSqlCriteria({ criteria });
47
-
48
- expect(result).toBe("a = 'test'");
49
- });
50
-
51
- it('should return number query for number criteria', () => {
52
- const criteria = { a: 3 };
53
-
54
- const result = SpecificationService.getSqlCriteria({ criteria });
55
-
56
- expect(result).toBe('a = 3');
57
- });
58
-
59
- it('should return query with and key for many keys', () => {
60
- const criteria = { a: 3, b: 'test' };
61
-
62
- const result = SpecificationService.getSqlCriteria({ criteria });
63
-
64
- expect(result).toBe("a = 3 and b = 'test'");
65
- });
66
- });
67
- });
@@ -1,85 +0,0 @@
1
- export interface ISpecificationCustom {
2
- $root?: any;
3
- }
4
-
5
- export const SPECIFICATION_ROOT_KEY = '$root.';
6
-
7
- export class SpecificationService {
8
- /**
9
- * Checking if the value meets the specifications
10
- * @param value {object} - object to check
11
- * @param spec {ISpecificatio} - specification
12
- * @param custom {ISpecificationCustom} - custom data for valid
13
- */
14
- static valid<T>(
15
- value: T,
16
- spec: { criteria: any },
17
- custom?: ISpecificationCustom,
18
- ): boolean {
19
- const keys = Object.keys(spec.criteria);
20
-
21
- if (!value) return false;
22
-
23
- for (let index = 0; index < keys.length; index++) {
24
- const key = keys[index];
25
-
26
- if (key.indexOf(SPECIFICATION_ROOT_KEY) === 0 && custom?.$root) {
27
- if (
28
- !SpecificationService.check(
29
- custom?.$root[key.replace(SPECIFICATION_ROOT_KEY, '')],
30
- spec.criteria[key],
31
- )
32
- )
33
- return false;
34
- } else if (
35
- !SpecificationService.check((value as any)[key], spec.criteria[key])
36
- )
37
- return false;
38
- }
39
-
40
- return true;
41
- }
42
-
43
- /**
44
- * Checking if the object does not meet the specifications
45
- * @param value {object} - object to check
46
- * @param spec {ISpecificatio} - specification
47
- * @param custom {ISpecificationCustom} - custom data for invalid
48
- */
49
- static invalid<T>(
50
- value: T,
51
- spec: { criteria: any },
52
- custom?: ISpecificationCustom,
53
- ): boolean {
54
- return !SpecificationService.valid(value, spec, custom);
55
- }
56
-
57
- /**
58
- * Convert specification to sql
59
- * @param spec {ISpecificatio} - specification
60
- * @return - sql criteria
61
- */
62
- static getSqlCriteria(spec: { criteria: any }): string {
63
- const keys = Object.keys(spec.criteria);
64
- let result = '';
65
-
66
- for (let index = 0; index < keys.length; index++) {
67
- const key = keys[index];
68
- const val = spec.criteria[key];
69
-
70
- if (index) result += ' and ';
71
-
72
- result += `${key} = ${typeof val === 'number' ? val : "'" + val + "'"}`;
73
- }
74
-
75
- return result;
76
- }
77
-
78
- private static check(value: any, criteriaValue: any): boolean {
79
- if (value && Array.isArray(value)) {
80
- return value.some((i) => i === criteriaValue);
81
- }
82
-
83
- return value === criteriaValue;
84
- }
85
- }
@@ -1,31 +0,0 @@
1
- import { ZipCodeService } from './zip-code.service';
2
-
3
- describe('shared-utils: ZipCodeService', () => {
4
- describe('isValid()', () => {
5
- it('should return true when correct zip code', () => {
6
- const result = ZipCodeService.isValid('01-123');
7
-
8
- expect(result).toBeTruthy();
9
- });
10
-
11
- it('should return false when incorrect zip code', () => {
12
- const result = ZipCodeService.isValid('123123');
13
-
14
- expect(result).not.toBeTruthy();
15
- });
16
- });
17
-
18
- describe('isInvalid()', () => {
19
- it('should return false when correct zip code', () => {
20
- const result = ZipCodeService.isInvalid('01-123');
21
-
22
- expect(result).not.toBeTruthy();
23
- });
24
-
25
- it('should return true when incorrect zip code', () => {
26
- const result = ZipCodeService.isInvalid('123');
27
-
28
- expect(result).toBeTruthy();
29
- });
30
- });
31
- });
@@ -1,20 +0,0 @@
1
- export class ZipCodeService {
2
- /***
3
- * Check valid zip code format
4
- * @param code {string} - zip code string
5
- * @return return true when correct
6
- */
7
- static isValid(code: string): boolean {
8
- const re = new RegExp('^\\d\\d-\\d\\d\\d$');
9
- return !!code.match(re);
10
- }
11
-
12
- /***
13
- * Check invalid zip code format
14
- * @param code {string} - zip code string
15
- * @return return true when incorrect
16
- */
17
- static isInvalid(code: string): boolean {
18
- return !ZipCodeService.isValid(code);
19
- }
20
- }
package/tsconfig.json DELETED
@@ -1,22 +0,0 @@
1
- {
2
- "extends": "../../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "module": "commonjs",
5
- "forceConsistentCasingInFileNames": true,
6
- "strict": true,
7
- "noImplicitOverride": true,
8
- "noPropertyAccessFromIndexSignature": true,
9
- "noImplicitReturns": true,
10
- "noFallthroughCasesInSwitch": true
11
- },
12
- "files": [],
13
- "include": [],
14
- "references": [
15
- {
16
- "path": "./tsconfig.lib.json"
17
- },
18
- {
19
- "path": "./tsconfig.spec.json"
20
- }
21
- ]
22
- }