@transferwise/neptune-validation 3.0.13 → 3.2.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 (35) hide show
  1. package/dist/common/keyMap.d.ts +19 -0
  2. package/dist/common/keyMap.d.ts.map +1 -0
  3. package/dist/event-validators/index.d.ts +2 -0
  4. package/dist/event-validators/index.d.ts.map +1 -0
  5. package/dist/event-validators/isKey.d.ts +6 -0
  6. package/dist/event-validators/isKey.d.ts.map +1 -0
  7. package/dist/index.d.ts +4 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.esm.js +59 -0
  10. package/dist/index.esm.js.map +1 -0
  11. package/dist/index.js +70 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/type-validators/index.d.ts +9 -0
  14. package/dist/type-validators/index.d.ts.map +1 -0
  15. package/dist/value-validators/index.d.ts +5 -0
  16. package/dist/value-validators/index.d.ts.map +1 -0
  17. package/package.json +26 -15
  18. package/src/common/keyMap.ts +6 -0
  19. package/src/event-validators/index.ts +1 -0
  20. package/src/event-validators/isKey.spec.ts +58 -0
  21. package/src/event-validators/isKey.ts +24 -0
  22. package/src/index.ts +3 -0
  23. package/src/type-validators/index.spec.ts +140 -0
  24. package/src/type-validators/index.ts +13 -0
  25. package/src/value-validators/index.spec.ts +23 -0
  26. package/src/value-validators/index.ts +8 -0
  27. package/build/es/polyfill/common/keyMap.js +0 -1
  28. package/build/es/polyfill/event-validators/index.js +0 -1
  29. package/build/es/polyfill/event-validators/isKey.js +0 -1
  30. package/build/es/polyfill/index.js +0 -1
  31. package/build/es/polyfill/type-validators/index.js +0 -1
  32. package/build/es/polyfill/type-validators/spec.js +0 -1
  33. package/build/es/polyfill/value-validators/index.js +0 -5
  34. package/build/es/polyfill/value-validators/spec.js +0 -1
  35. package/build/umd/polyfill/main.js +0 -1
@@ -0,0 +1,19 @@
1
+ export declare const keyMap: {
2
+ readonly SPACE: {
3
+ readonly key: readonly [" ", "Spacebar"];
4
+ readonly keyCode: 32;
5
+ };
6
+ readonly ENTER: {
7
+ readonly key: "Enter";
8
+ readonly keyCode: 13;
9
+ };
10
+ readonly TAB: {
11
+ readonly key: "Tab";
12
+ readonly keyCode: 9;
13
+ };
14
+ readonly ESCAPE: {
15
+ readonly key: "Escape";
16
+ readonly keyCode: 27;
17
+ };
18
+ };
19
+ //# sourceMappingURL=keyMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyMap.d.ts","sourceRoot":"","sources":["../../src/common/keyMap.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;CAKT,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { default } from './isKey';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/event-validators/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC"}
@@ -0,0 +1,6 @@
1
+ declare const isKey: ({ keyType, event }: {
2
+ keyType?: string | undefined;
3
+ event?: Partial<KeyboardEvent> | null | undefined;
4
+ }) => boolean;
5
+ export default isKey;
6
+ //# sourceMappingURL=isKey.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isKey.d.ts","sourceRoot":"","sources":["../../src/event-validators/isKey.ts"],"names":[],"mappings":"AAGA,QAAA,MAAM,KAAK;;;aAkBV,CAAC;AAEF,eAAe,KAAK,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from './type-validators';
2
+ export * from './value-validators';
3
+ export { default as isKey } from './event-validators';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,oBAAoB,CAAC"}
@@ -0,0 +1,59 @@
1
+ const isString = value => typeof value === 'string';
2
+ const isNumber = value => typeof value === 'number' && !Number.isNaN(value);
3
+ const isInteger = value => {
4
+ return isNumber(value) && Math.floor(value) === value;
5
+ };
6
+ const isBoolean = value => typeof value === 'boolean';
7
+ const isObject = value => typeof value === 'object' && !isNull(value) && value.constructor === Object;
8
+ const isArray = value => Array.isArray(value);
9
+ const isNull = value => value === null;
10
+ const isUndefined = value => typeof value === 'undefined';
11
+
12
+ /**
13
+ * Checks empty values for arrays, objects and strings.
14
+ */
15
+ const isEmpty = value => isString(value) && value.length === 0 || (isObject(value) || isArray(value)) && Object.keys(value).length === 0;
16
+
17
+ const keyMap = {
18
+ SPACE: {
19
+ key: [' ', 'Spacebar'],
20
+ keyCode: 32
21
+ },
22
+ ENTER: {
23
+ key: 'Enter',
24
+ keyCode: 13
25
+ },
26
+ TAB: {
27
+ key: 'Tab',
28
+ keyCode: 9
29
+ },
30
+ ESCAPE: {
31
+ key: 'Escape',
32
+ keyCode: 27
33
+ }
34
+ };
35
+
36
+ const isKey = ({
37
+ keyType,
38
+ event
39
+ }) => {
40
+ if (!keyType || !event) {
41
+ return false;
42
+ }
43
+ const upperCaseKeyType = keyType.toUpperCase();
44
+ if (!Object.hasOwn(keyMap, upperCaseKeyType)) {
45
+ return false;
46
+ }
47
+ const keyDef = keyMap[upperCaseKeyType];
48
+ const {
49
+ key,
50
+ keyCode
51
+ } = event;
52
+ if (key) {
53
+ return isArray(keyDef.key) ? keyDef.key.includes(key) : key === keyDef.key;
54
+ }
55
+ return keyCode === keyDef.keyCode;
56
+ };
57
+
58
+ export { isArray, isBoolean, isEmpty, isInteger, isKey, isNull, isNumber, isObject, isString, isUndefined };
59
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":["../src/type-validators/index.ts","../src/value-validators/index.ts","../src/common/keyMap.ts","../src/event-validators/isKey.ts"],"sourcesContent":[null,null,null,null],"names":["isString","value","isNumber","isNaN","isInteger","Math","floor","isBoolean","isObject","isNull","constructor","Object","isArray","Array","keyMap","event"],"mappings":"AAAA,MAAAA,QAAa,GAAAC,KAAmB,IAAA,OAAAA;MAChCC,QAAa,GAAAD,KAAmB,IAAA,OAAAA,6BACoB,CAAAE,KAAA,CAAAF,KAAA,EAAA;AACpDG,MAAAA,SAAa,GAAAH,KAAoB,IAAA;AAGjC,EAAA,OAAAC,QAAa,CAAAD,KAAA,CAAA,IAASI,IAAW,CAAAC,KAAA,CAAAL,KAAA,CAAO;AAExC,EAAA;AAEA,MAAAM,SAAa,GAAAN,KAAkB,IAAA,OAAAA;AAC/B,MAAAO,QAAa,GAAAP,KAAiB,IAAA,OAAAA,sBAA0C,CAAAQ,MAAA,CAAAR,KAAA,CAAA,IAAAA,KAAA,CAAAS,WAAA,KAAAC,OAAA;AACxE,MAAAC,OAAa,GAAAX,KAAA,IAAsBY,KAAA,CAAAD,OAAA,CAAAX;;;;;ACRhC;AACH;;;ACLA,MAAAa,SAAmB;;;;;;;;;;;;;;;;;;;;;;;cCqBjB,IAAA,CAAAC,KAAA,EAAA;AAEF,IAAA,OAAe,KAAK,CAAA;;;;;;;;;;;;;;;;;;;"}
package/dist/index.js ADDED
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ const isString = value => typeof value === 'string';
4
+ const isNumber = value => typeof value === 'number' && !Number.isNaN(value);
5
+ const isInteger = value => {
6
+ return isNumber(value) && Math.floor(value) === value;
7
+ };
8
+ const isBoolean = value => typeof value === 'boolean';
9
+ const isObject = value => typeof value === 'object' && !isNull(value) && value.constructor === Object;
10
+ const isArray = value => Array.isArray(value);
11
+ const isNull = value => value === null;
12
+ const isUndefined = value => typeof value === 'undefined';
13
+
14
+ /**
15
+ * Checks empty values for arrays, objects and strings.
16
+ */
17
+ const isEmpty = value => isString(value) && value.length === 0 || (isObject(value) || isArray(value)) && Object.keys(value).length === 0;
18
+
19
+ const keyMap = {
20
+ SPACE: {
21
+ key: [' ', 'Spacebar'],
22
+ keyCode: 32
23
+ },
24
+ ENTER: {
25
+ key: 'Enter',
26
+ keyCode: 13
27
+ },
28
+ TAB: {
29
+ key: 'Tab',
30
+ keyCode: 9
31
+ },
32
+ ESCAPE: {
33
+ key: 'Escape',
34
+ keyCode: 27
35
+ }
36
+ };
37
+
38
+ const isKey = ({
39
+ keyType,
40
+ event
41
+ }) => {
42
+ if (!keyType || !event) {
43
+ return false;
44
+ }
45
+ const upperCaseKeyType = keyType.toUpperCase();
46
+ if (!Object.hasOwn(keyMap, upperCaseKeyType)) {
47
+ return false;
48
+ }
49
+ const keyDef = keyMap[upperCaseKeyType];
50
+ const {
51
+ key,
52
+ keyCode
53
+ } = event;
54
+ if (key) {
55
+ return isArray(keyDef.key) ? keyDef.key.includes(key) : key === keyDef.key;
56
+ }
57
+ return keyCode === keyDef.keyCode;
58
+ };
59
+
60
+ exports.isArray = isArray;
61
+ exports.isBoolean = isBoolean;
62
+ exports.isEmpty = isEmpty;
63
+ exports.isInteger = isInteger;
64
+ exports.isKey = isKey;
65
+ exports.isNull = isNull;
66
+ exports.isNumber = isNumber;
67
+ exports.isObject = isObject;
68
+ exports.isString = isString;
69
+ exports.isUndefined = isUndefined;
70
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/type-validators/index.ts","../src/value-validators/index.ts","../src/common/keyMap.ts","../src/event-validators/isKey.ts"],"sourcesContent":[null,null,null,null],"names":["isString","value","isNumber","isNaN","isInteger","Math","floor","isBoolean","isObject","isNull","constructor","Object","isArray","Array","keyMap","event"],"mappings":";;AAAA,MAAAA,QAAa,GAAAC,KAAmB,IAAA,OAAAA;MAChCC,QAAa,GAAAD,KAAmB,IAAA,OAAAA,6BACoB,CAAAE,KAAA,CAAAF,KAAA,EAAA;AACpDG,MAAAA,SAAa,GAAAH,KAAoB,IAAA;AAGjC,EAAA,OAAAC,QAAa,CAAAD,KAAA,CAAA,IAASI,IAAW,CAAAC,KAAA,CAAAL,KAAA,CAAO;AAExC,EAAA;AAEA,MAAAM,SAAa,GAAAN,KAAkB,IAAA,OAAAA;AAC/B,MAAAO,QAAa,GAAAP,KAAiB,IAAA,OAAAA,sBAA0C,CAAAQ,MAAA,CAAAR,KAAA,CAAA,IAAAA,KAAA,CAAAS,WAAA,KAAAC,OAAA;AACxE,MAAAC,OAAa,GAAAX,KAAA,IAAsBY,KAAA,CAAAD,OAAA,CAAAX;;;;;ACRhC;AACH;;;ACLA,MAAAa,SAAmB;;;;;;;;;;;;;;;;;;;;;;;cCqBjB,IAAA,CAAAC,KAAA,EAAA;AAEF,IAAA,OAAe,KAAK,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,9 @@
1
+ export declare const isString: (value: unknown) => value is string;
2
+ export declare const isNumber: (value: unknown) => value is number;
3
+ export declare const isInteger: (value: unknown) => value is number;
4
+ export declare const isBoolean: (value: unknown) => value is boolean;
5
+ export declare const isObject: (value: unknown) => value is Record<string, unknown>;
6
+ export declare const isArray: (value: unknown) => value is unknown[];
7
+ export declare const isNull: (value: unknown) => value is null;
8
+ export declare const isUndefined: (value: unknown) => value is undefined;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/type-validators/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,UAAW,OAAO,oBAA+C,CAAC;AACvF,eAAO,MAAM,QAAQ,UAAW,OAAO,oBACY,CAAC;AACpD,eAAO,MAAM,SAAS,UAAW,OAAO,oBAEvC,CAAC;AACF,eAAO,MAAM,SAAS,UAAW,OAAO,qBAAiD,CAAC;AAE1F,eAAO,MAAM,QAAQ,UAAW,OAAO,qCACsC,CAAC;AAC9E,eAAO,MAAM,OAAO,UAAW,OAAO,uBAA6C,CAAC;AACpF,eAAO,MAAM,MAAM,UAAW,OAAO,kBAAkC,CAAC;AACxE,eAAO,MAAM,WAAW,UAAW,OAAO,uBAAqD,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Checks empty values for arrays, objects and strings.
3
+ */
4
+ export declare const isEmpty: (value: unknown) => boolean;
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/value-validators/index.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,OAAO,UAAW,OAAO,YAEoC,CAAC"}
package/package.json CHANGED
@@ -1,13 +1,8 @@
1
1
  {
2
2
  "name": "@transferwise/neptune-validation",
3
- "version": "3.0.13",
3
+ "version": "3.2.0",
4
4
  "description": "Neptune Web validation",
5
5
  "license": "Apache-2.0",
6
- "main": "./build/umd/polyfill/main.js",
7
- "module": "./build/es/polyfill/index.js",
8
- "files": [
9
- "build"
10
- ],
11
6
  "keywords": [
12
7
  "wise",
13
8
  "neptune-web-validation"
@@ -17,19 +12,36 @@
17
12
  "fullname": "transferwise/neptune-web",
18
13
  "url": "git+https://github.com/transferwise/neptune-web.git"
19
14
  },
15
+ "author": "Wise Payments Ltd.",
16
+ "sideEffects": [
17
+ "*.css"
18
+ ],
19
+ "main": "./dist/index.js",
20
+ "module": "./dist/index.esm.js",
21
+ "types": "./dist/index.d.ts",
22
+ "files": [
23
+ "dist/",
24
+ "src/",
25
+ "!**/*.tsbuildinfo"
26
+ ],
20
27
  "dependencies": {
21
- "@babel/runtime": "^7.18.3",
28
+ "@babel/runtime": "^7.22.15",
22
29
  "core-js": "^3.8.0"
23
30
  },
24
31
  "devDependencies": {
25
- "@rollup/plugin-babel": "^5.2.2",
26
- "@rollup/plugin-commonjs": "^11.0.2",
27
- "@rollup/plugin-node-resolve": "^7.1.3",
32
+ "@babel/core": "^7.22.15",
33
+ "@babel/plugin-transform-runtime": "^7.22.15",
34
+ "@babel/preset-env": "^7.22.15",
35
+ "@babel/preset-typescript": "^7.22.15",
36
+ "@rollup/plugin-babel": "^6.0.3",
37
+ "@rollup/plugin-node-resolve": "^15.2.1",
38
+ "@rollup/plugin-typescript": "^11.1.3",
28
39
  "@transferwise/eslint-config": "^7.4.0",
40
+ "@tsconfig/recommended": "^1.0.2",
41
+ "@types/babel__core": "^7.20.1",
29
42
  "@types/jest": "^27.5.2",
30
43
  "jest": "^27.0.6",
31
- "rollup": "^2.34.0",
32
- "rollup-plugin-uglify": "^6.0.4"
44
+ "rollup": "^3.28.1"
33
45
  },
34
46
  "publishConfig": {
35
47
  "access": "public"
@@ -45,8 +57,7 @@
45
57
  "lint:fix:format": "prettier --write --ignore-path ../../../.prettierignore .",
46
58
  "lint:fix:js+ts": "pnpm run lint:check:js+ts --fix",
47
59
  "build": "npm-run-all build:*",
48
- "build:clean": "rm -rf build",
49
- "build:umd": "NODE_ENV=umd rollup -c",
50
- "build:es": "NODE_ENV=es babel src -d build/es/polyfill --ignore '**/*.spec.js','**/*.story.js'"
60
+ "build:clean": "rm -rf dist",
61
+ "build:js": "rollup --config --sourcemap"
51
62
  }
52
63
  }
@@ -0,0 +1,6 @@
1
+ export const keyMap = {
2
+ SPACE: { key: [' ', 'Spacebar'], keyCode: 32 },
3
+ ENTER: { key: 'Enter', keyCode: 13 },
4
+ TAB: { key: 'Tab', keyCode: 9 },
5
+ ESCAPE: { key: 'Escape', keyCode: 27 },
6
+ } as const;
@@ -0,0 +1 @@
1
+ export { default } from './isKey';
@@ -0,0 +1,58 @@
1
+ /* eslint-disable no-console */
2
+ import { keyMap } from '../common/keyMap';
3
+
4
+ import isKey from '.';
5
+
6
+ const { TAB, ESCAPE } = keyMap;
7
+
8
+ describe('isKey', () => {
9
+ it('returns true if eventKey is equal to key', () => {
10
+ const event = { key: 'Tab', keyCode: TAB.keyCode };
11
+ expect(isKey({ keyType: TAB.key, event })).toBe(true);
12
+ });
13
+
14
+ it('returns true if eventKey is equal to empty key', () => {
15
+ const event = { key: ' ' };
16
+ expect(isKey({ keyType: 'Space', event })).toBe(true);
17
+ });
18
+
19
+ it('returns true if eventKey is equal to Spacebar', () => {
20
+ const event = { key: 'Spacebar' };
21
+ expect(isKey({ keyType: 'Space', event })).toBe(true);
22
+ });
23
+
24
+ it('returns true if eventKey is equal to Tab', () => {
25
+ const event = { key: 'Tab' };
26
+ expect(isKey({ keyType: TAB.key, event })).toBe(true);
27
+ });
28
+
29
+ it('returns true if eventKeyCode is equal to key', () => {
30
+ const event = { keyCode: TAB.keyCode };
31
+ expect(isKey({ keyType: TAB.key, event })).toBe(true);
32
+ });
33
+
34
+ it('returns false if key is not defined', () => {
35
+ const event = { keyCode: TAB.keyCode };
36
+ expect(isKey({ event })).toBe(false);
37
+ });
38
+
39
+ it('returns false if event is not defined', () => {
40
+ const event = null;
41
+ expect(isKey({ event })).toBe(false);
42
+ });
43
+
44
+ it('returns false if eventKey or eventKeyCode are not defined', () => {
45
+ const event = {};
46
+ expect(isKey({ keyType: TAB.key, event })).toBe(false);
47
+ });
48
+
49
+ it('returns false if eventKey is equal to key', () => {
50
+ const event = { key: 'Escape', keyCode: ESCAPE.keyCode };
51
+ expect(isKey({ keyType: TAB.key, event })).toBe(false);
52
+ });
53
+
54
+ it('returns false if eventKeyCode is equal to key', () => {
55
+ const event = { keyCode: ESCAPE.keyCode };
56
+ expect(isKey({ keyType: TAB.key, event })).toBe(false);
57
+ });
58
+ });
@@ -0,0 +1,24 @@
1
+ import { keyMap } from '../common/keyMap';
2
+ import { isArray } from '../type-validators';
3
+
4
+ const isKey = ({ keyType, event }: { keyType?: string; event?: Partial<KeyboardEvent> | null }) => {
5
+ if (!keyType || !event) {
6
+ return false;
7
+ }
8
+
9
+ const upperCaseKeyType = keyType.toUpperCase();
10
+ if (!Object.hasOwn(keyMap, upperCaseKeyType)) {
11
+ return false;
12
+ }
13
+ const keyDef = keyMap[upperCaseKeyType as keyof typeof keyMap];
14
+
15
+ const { key, keyCode } = event;
16
+
17
+ if (key) {
18
+ return isArray(keyDef.key) ? keyDef.key.includes(key) : key === keyDef.key;
19
+ }
20
+
21
+ return keyCode === keyDef.keyCode;
22
+ };
23
+
24
+ export default isKey;
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './type-validators';
2
+ export * from './value-validators';
3
+ export { default as isKey } from './event-validators';
@@ -0,0 +1,140 @@
1
+ import {
2
+ isString,
3
+ isNumber,
4
+ isInteger,
5
+ isBoolean,
6
+ isArray,
7
+ isObject,
8
+ isNull,
9
+ isUndefined,
10
+ } from '.';
11
+
12
+ describe('given a library for validating data types', () => {
13
+ describe('when validating a string', () => {
14
+ it('should return true when the value is a string', () => {
15
+ expect(isString('a')).toBe(true);
16
+ expect(isString('')).toBe(true);
17
+ });
18
+ it('should return false when the value is not string', () => {
19
+ expect(isString(1)).toBe(false);
20
+ expect(isString(true)).toBe(false);
21
+ expect(isString([])).toBe(false);
22
+ expect(isString({})).toBe(false);
23
+ expect(isString(undefined)).toBe(false);
24
+ expect(isString(null)).toBe(false);
25
+ expect(isString(NaN)).toBe(false);
26
+ });
27
+ });
28
+
29
+ describe('when validating a number', () => {
30
+ it('should return true when the value is a number', () => {
31
+ expect(isNumber(1)).toBe(true);
32
+ expect(isNumber(0)).toBe(true);
33
+ expect(isNumber(1.23)).toBe(true);
34
+ expect(isNumber(-1)).toBe(true);
35
+ });
36
+ it('should return false when the value is not number', () => {
37
+ expect(isNumber('a')).toBe(false);
38
+ expect(isNumber(true)).toBe(false);
39
+ expect(isNumber([])).toBe(false);
40
+ expect(isNumber({})).toBe(false);
41
+ expect(isNumber(undefined)).toBe(false);
42
+ expect(isNumber(null)).toBe(false);
43
+ expect(isNumber(NaN)).toBe(false);
44
+ });
45
+ });
46
+
47
+ describe('when validating an integer', () => {
48
+ it('should return true when the value is a integer', () => {
49
+ expect(isInteger(1)).toBe(true);
50
+ expect(isInteger(0)).toBe(true);
51
+ expect(isInteger(-1)).toBe(true);
52
+ });
53
+ it('should return false when the value is not an integer', () => {
54
+ expect(isInteger(1.23)).toBe(false);
55
+ expect(isInteger('a')).toBe(false);
56
+ expect(isInteger(true)).toBe(false);
57
+ expect(isInteger([])).toBe(false);
58
+ expect(isInteger({})).toBe(false);
59
+ expect(isInteger(undefined)).toBe(false);
60
+ expect(isInteger(null)).toBe(false);
61
+ expect(isInteger(NaN)).toBe(false);
62
+ });
63
+ });
64
+
65
+ describe('when validating a boolean', () => {
66
+ it('should return true when the value is a boolean', () => {
67
+ expect(isBoolean(true)).toBe(true);
68
+ expect(isBoolean(false)).toBe(true);
69
+ });
70
+ it('should return false when the value is not a boolean', () => {
71
+ expect(isBoolean(1)).toBe(false);
72
+ expect(isBoolean('a')).toBe(false);
73
+ expect(isBoolean([])).toBe(false);
74
+ expect(isBoolean({})).toBe(false);
75
+ expect(isBoolean(undefined)).toBe(false);
76
+ expect(isBoolean(null)).toBe(false);
77
+ expect(isBoolean(NaN)).toBe(false);
78
+ });
79
+ });
80
+
81
+ describe('when validating an array', () => {
82
+ it('should return true when the value is an array', () => {
83
+ expect(isArray([1])).toBe(true);
84
+ expect(isArray([])).toBe(true);
85
+ });
86
+ it('should return false when the value is not an array', () => {
87
+ expect(isArray(1)).toBe(false);
88
+ expect(isArray('a')).toBe(false);
89
+ expect(isArray(true)).toBe(false);
90
+ expect(isArray({})).toBe(false);
91
+ expect(isArray(undefined)).toBe(false);
92
+ expect(isArray(null)).toBe(false);
93
+ expect(isArray(NaN)).toBe(false);
94
+ });
95
+ });
96
+
97
+ describe('when validating an object', () => {
98
+ it('should return true when the value is an object', () => {
99
+ expect(isObject({ a: 1 })).toBe(true);
100
+ expect(isObject({})).toBe(true);
101
+ });
102
+ it('should return false when the value is not an object', () => {
103
+ expect(isObject(1)).toBe(false);
104
+ expect(isObject('a')).toBe(false);
105
+ expect(isObject(true)).toBe(false);
106
+ expect(isObject([])).toBe(false);
107
+ expect(isObject(undefined)).toBe(false);
108
+ expect(isObject(null)).toBe(false);
109
+ expect(isObject(NaN)).toBe(false);
110
+ });
111
+ });
112
+
113
+ describe('when validating a null', () => {
114
+ it('should return true when the value is null', () => {
115
+ expect(isNull(null)).toBe(true);
116
+ });
117
+ it('should return false when the value is not null', () => {
118
+ expect(isNull(0)).toBe(false);
119
+ expect(isNull(false)).toBe(false);
120
+ expect(isNull([])).toBe(false);
121
+ expect(isNull({})).toBe(false);
122
+ expect(isNull(undefined)).toBe(false);
123
+ expect(isNull(NaN)).toBe(false);
124
+ });
125
+ });
126
+
127
+ describe('when validating an undefined', () => {
128
+ it('should return true when the value is undefined', () => {
129
+ expect(isUndefined(undefined)).toBe(true);
130
+ });
131
+ it('should return false when the value is not undefined', () => {
132
+ expect(isUndefined(0)).toBe(false);
133
+ expect(isUndefined(false)).toBe(false);
134
+ expect(isUndefined([])).toBe(false);
135
+ expect(isUndefined({})).toBe(false);
136
+ expect(isUndefined(null)).toBe(false);
137
+ expect(isUndefined(NaN)).toBe(false);
138
+ });
139
+ });
140
+ });
@@ -0,0 +1,13 @@
1
+ export const isString = (value: unknown): value is string => typeof value === 'string';
2
+ export const isNumber = (value: unknown): value is number =>
3
+ typeof value === 'number' && !Number.isNaN(value);
4
+ export const isInteger = (value: unknown): value is number => {
5
+ return isNumber(value) && Math.floor(value) === value;
6
+ };
7
+ export const isBoolean = (value: unknown): value is boolean => typeof value === 'boolean';
8
+
9
+ export const isObject = (value: unknown): value is Record<string, unknown> =>
10
+ typeof value === 'object' && !isNull(value) && value.constructor === Object;
11
+ export const isArray = (value: unknown): value is unknown[] => Array.isArray(value);
12
+ export const isNull = (value: unknown): value is null => value === null;
13
+ export const isUndefined = (value: unknown): value is undefined => typeof value === 'undefined';
@@ -0,0 +1,23 @@
1
+ import { isEmpty } from '.';
2
+
3
+ describe('given a library for validating values', () => {
4
+ describe('when checking for empty values', () => {
5
+ it('should return true for empty strings', () => {
6
+ expect(isEmpty('')).toBe(true);
7
+ expect(isEmpty('a')).toBe(false);
8
+ });
9
+ it('should return true for empty array', () => {
10
+ expect(isEmpty([])).toBe(true);
11
+ expect(isEmpty(['a'])).toBe(false);
12
+ });
13
+ it('should return true for empty object', () => {
14
+ expect(isEmpty({})).toBe(true);
15
+ expect(isEmpty({ a: 'a' })).toBe(false);
16
+ });
17
+ it('should return false when the value is not a string, array, or object', () => {
18
+ expect(isEmpty(1)).toBe(false);
19
+ expect(isEmpty(null)).toBe(false);
20
+ expect(isEmpty(undefined)).toBe(false);
21
+ });
22
+ });
23
+ });
@@ -0,0 +1,8 @@
1
+ import { isString, isObject, isArray } from '../type-validators';
2
+
3
+ /**
4
+ * Checks empty values for arrays, objects and strings.
5
+ */
6
+ export const isEmpty = (value: unknown) =>
7
+ (isString(value) && value.length === 0) ||
8
+ ((isObject(value) || isArray(value)) && Object.keys(value).length === 0);
@@ -1 +0,0 @@
1
- export var keyMap={SPACE:{key:[" ","Spacebar"],keyCode:32},ENTER:{key:"Enter",keyCode:13},TAB:{key:"Tab",keyCode:9},ESCAPE:{key:"Escape",keyCode:27}};
@@ -1 +0,0 @@
1
- export{default}from"./isKey";
@@ -1 +0,0 @@
1
- import"core-js/modules/es.array.includes.js";import"core-js/modules/es.string.includes.js";import{keyMap}from"../common/keyMap";import{isArray}from"../type-validators";var isKey=function(a){var b=a.keyType,c=a.event;if(!b||!c)return!1;var d=b.toUpperCase(),e=keyMap[d];if(!e)return!1;var f=c.key,g=c.keyCode;return f?isArray(e.key)?e.key.includes(f):f===e.key:g===e.keyCode};export default isKey;
@@ -1 +0,0 @@
1
- export*from"./type-validators";export*from"./value-validators";export{default as isKey}from"./event-validators";
@@ -1 +0,0 @@
1
- import"core-js/modules/es.number.is-nan.js";import"core-js/modules/es.number.constructor.js";var isString=function(a){return"string"==typeof a},isNumber=function(a){return"number"==typeof a&&!Number.isNaN(a)},isInteger=function(a){return isNumber(a)&&Math.floor(a)===a},isBoolean=function(a){return"boolean"==typeof a},isObject=function(a){return!isNull(a)&&!isUndefined(a)&&a.constructor===Object},isArray=function(a){return Array.isArray(a)},isNull=function(a){return null===a},isUndefined=function(a){return"undefined"==typeof a};export{isString,isNumber,isInteger,isBoolean,isObject,isArray,isNull,isUndefined};
@@ -1 +0,0 @@
1
- import{isString,isNumber,isInteger,isBoolean,isArray,isObject,isNull,isUndefined}from".";describe("Given a library for validating data types",function(){describe("when validating a string",function(){it("should return true when the value is a string",function(){expect(isString("a")).toBe(!0),expect(isString("")).toBe(!0)}),it("should return false when the value is not string",function(){expect(isString(1)).toBe(!1),expect(isString(!0)).toBe(!1),expect(isString([])).toBe(!1),expect(isString({})).toBe(!1),expect(isString(void 0)).toBe(!1),expect(isString(null)).toBe(!1),expect(isString(NaN)).toBe(!1)})}),describe("when validating a number",function(){it("should return true when the value is a number",function(){expect(isNumber(1)).toBe(!0),expect(isNumber(0)).toBe(!0),expect(isNumber(1.23)).toBe(!0),expect(isNumber(-1)).toBe(!0)}),it("should return false when the value is not number",function(){expect(isNumber("a")).toBe(!1),expect(isNumber(!0)).toBe(!1),expect(isNumber([])).toBe(!1),expect(isNumber({})).toBe(!1),expect(isNumber(void 0)).toBe(!1),expect(isNumber(null)).toBe(!1),expect(isNumber(NaN)).toBe(!1)})}),describe("when validating an integer",function(){it("should return true when the value is a integer",function(){expect(isInteger(1)).toBe(!0),expect(isInteger(0)).toBe(!0),expect(isInteger(-1)).toBe(!0)}),it("should return false when the value is not an integer",function(){expect(isInteger(1.23)).toBe(!1),expect(isInteger("a")).toBe(!1),expect(isInteger(!0)).toBe(!1),expect(isInteger([])).toBe(!1),expect(isInteger({})).toBe(!1),expect(isInteger(void 0)).toBe(!1),expect(isInteger(null)).toBe(!1),expect(isInteger(NaN)).toBe(!1)})}),describe("when validating a boolean",function(){it("should return true when the value is a boolean",function(){expect(isBoolean(!0)).toBe(!0),expect(isBoolean(!1)).toBe(!0)}),it("should return false when the value is not a boolean",function(){expect(isBoolean(1)).toBe(!1),expect(isBoolean("a")).toBe(!1),expect(isBoolean([])).toBe(!1),expect(isBoolean({})).toBe(!1),expect(isBoolean(void 0)).toBe(!1),expect(isBoolean(null)).toBe(!1),expect(isBoolean(NaN)).toBe(!1)})}),describe("when validating an array",function(){it("should return true when the value is an array",function(){expect(isArray([1])).toBe(!0),expect(isArray([])).toBe(!0)}),it("should return false when the value is not an array",function(){expect(isArray(1)).toBe(!1),expect(isArray("a")).toBe(!1),expect(isArray(!0)).toBe(!1),expect(isArray({})).toBe(!1),expect(isArray(void 0)).toBe(!1),expect(isArray(null)).toBe(!1),expect(isArray(NaN)).toBe(!1)})}),describe("when validating an object",function(){it("should return true when the value is an object",function(){expect(isObject({a:1})).toBe(!0),expect(isObject({})).toBe(!0)}),it("should return false when the value is not an object",function(){expect(isObject(1)).toBe(!1),expect(isObject("a")).toBe(!1),expect(isObject(!0)).toBe(!1),expect(isObject([])).toBe(!1),expect(isObject(void 0)).toBe(!1),expect(isObject(null)).toBe(!1),expect(isObject(NaN)).toBe(!1)})}),describe("when validating a null",function(){it("should return true when the value is null",function(){expect(isNull(null)).toBe(!0)}),it("should return false when the value is not null",function(){expect(isNull(0)).toBe(!1),expect(isNull(!1)).toBe(!1),expect(isNull([])).toBe(!1),expect(isNull({})).toBe(!1),expect(isNull(void 0)).toBe(!1),expect(isNull(NaN)).toBe(!1)})}),describe("when validating an undefined",function(){it("should return true when the value is undefined",function(){expect(isUndefined(void 0)).toBe(!0)}),it("should return false when the value is not undefined",function(){expect(isUndefined(0)).toBe(!1),expect(isUndefined(!1)).toBe(!1),expect(isUndefined([])).toBe(!1),expect(isUndefined({})).toBe(!1),expect(isUndefined(null)).toBe(!1),expect(isUndefined(NaN)).toBe(!1)})})});
@@ -1,5 +0,0 @@
1
- import"core-js/modules/es.object.keys.js";import{isString,isObject,isArray}from"../type-validators";/**
2
- * Checks empty values for arrays,objects and strings.
3
- *
4
- * @param {object | Array | string} value
5
- */var isEmpty=function(a){return isString(a)&&0===a.length||(isObject(a)||isArray(a))&&0===Object.keys(a).length};export{isEmpty};
@@ -1 +0,0 @@
1
- import{isEmpty}from".";describe("Given a library for validating values",function(){describe("when checking for empty values",function(){it("should return true for empty strings",function(){expect(isEmpty("")).toBe(!0),expect(isEmpty("a")).toBe(!1)}),it("should return true for empty array",function(){expect(isEmpty([])).toBe(!0),expect(isEmpty(["a"])).toBe(!1)}),it("should return true for empty object",function(){expect(isEmpty({})).toBe(!0),expect(isEmpty({a:"a"})).toBe(!1)}),it("should return false when the value is not a string, array, or object",function(){expect(isEmpty(1)).toBe(!1),expect(isEmpty(null)).toBe(!1),expect(isEmpty(void 0)).toBe(!1)})})});
@@ -1 +0,0 @@
1
- !function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("core-js/modules/es.number.is-nan.js"),require("core-js/modules/es.number.constructor.js"),require("core-js/modules/es.object.keys.js"),require("core-js/modules/es.array.includes.js"),require("core-js/modules/es.string.includes.js")):"function"==typeof define&&define.amd?define(["exports","core-js/modules/es.number.is-nan.js","core-js/modules/es.number.constructor.js","core-js/modules/es.object.keys.js","core-js/modules/es.array.includes.js","core-js/modules/es.string.includes.js"],r):r((e="undefined"!=typeof globalThis?globalThis:e||self)["@transferwise/neptune-validation"]={})}(this,function(e){"use strict";function r(e){return"string"==typeof e}function s(e){return"number"==typeof e&&!Number.isNaN(e)}function n(e){return!t(e)&&!u(e)&&e.constructor===Object}function o(e){return Array.isArray(e)}var t=function(e){return null===e},u=function(e){return void 0===e},i={SPACE:{key:[" ","Spacebar"],keyCode:32},ENTER:{key:"Enter",keyCode:13},TAB:{key:"Tab",keyCode:9},ESCAPE:{key:"Escape",keyCode:27}};e.isArray=o,e.isBoolean=function(e){return"boolean"==typeof e},e.isEmpty=function(e){return r(e)&&0===e.length||(n(e)||o(e))&&0===Object.keys(e).length},e.isInteger=function(e){return s(e)&&Math.floor(e)===e},e.isKey=function(e){var r=e.keyType,s=e.event;if(!r||!s)return!1;e=r.toUpperCase(),r=i[e];if(!r)return!1;e=s.key,s=s.keyCode;return e?o(r.key)?r.key.includes(e):e===r.key:s===r.keyCode},e.isNull=t,e.isNumber=s,e.isObject=n,e.isString=r,e.isUndefined=u,Object.defineProperty(e,"__esModule",{value:!0})});