@awesomeness-js/utils 1.1.4 → 1.1.6

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/index.js CHANGED
@@ -40,6 +40,7 @@ import _utils_generateNamespaceCode from './src/utils/generateNamespaceCode.js';
40
40
  import _utils_generateNamespaceExportLines from './src/utils/generateNamespaceExportLines.js';
41
41
  import _uuid from './src/uuid.js';
42
42
  import _validateSchema from './src/validateSchema.js';
43
+ import _wait from './src/wait.js';
43
44
 
44
45
  export { _build as build };
45
46
  export { _collectImports as collectImports };
@@ -58,6 +59,7 @@ export { _thingType as thingType };
58
59
  export { _toPennies as toPennies };
59
60
  export { _uuid as uuid };
60
61
  export { _validateSchema as validateSchema };
62
+ export { _wait as wait };
61
63
 
62
64
  export const clean = {
63
65
  array: _clean_array,
@@ -135,6 +137,7 @@ export default {
135
137
  toPennies: _toPennies,
136
138
  uuid: _uuid,
137
139
  validateSchema: _validateSchema,
140
+ wait: _wait,
138
141
  clean: {
139
142
  array: _clean_array,
140
143
  boolean: _clean_boolean,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awesomeness-js/utils",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Awesomeness - Utils",
5
5
  "repository": {
6
6
  "type": "git",
@@ -4,7 +4,9 @@ export default function cleanString(x, {
4
4
  maxLength = false,
5
5
  allowHtml = false,
6
6
  allowScripts = false,
7
- validValues = false
7
+ validValues = false,
8
+ format = false,
9
+ pattern = false
8
10
  } = {}){
9
11
 
10
12
  if(allowScripts && !allowHtml){
@@ -77,6 +79,53 @@ export default function cleanString(x, {
77
79
 
78
80
  }
79
81
 
82
+ if(format === 'email') {
83
+
84
+ const emailRegex = /^(?!@)(?!.*@.*@)(?!.*\.\.)(?!\.\.)(?!.*-$)(?!.*-$)(?!.*\.-)[A-Za-z0-9._%+-]+(?:@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,})$/;
85
+
86
+ if(!emailRegex.test(x)) {
87
+
88
+ throw {
89
+ message: 'Invalid email format',
90
+ value: x
91
+ };
92
+
93
+ }
94
+
95
+ }
96
+
97
+ if(pattern) {
98
+
99
+ // make sure pattern is itself a regex
100
+ if(!(pattern instanceof RegExp)) {
101
+
102
+ try {
103
+
104
+ pattern = new RegExp(pattern);
105
+
106
+ } catch (e) {
107
+
108
+ throw {
109
+ message: 'Pattern is not a valid regular expression',
110
+ pattern,
111
+ value: x
112
+ };
113
+
114
+ }
115
+
116
+ }
117
+
118
+ if(!pattern.test(x)) {
119
+
120
+ throw {
121
+ message: 'Invalid format',
122
+ value: x
123
+ };
124
+
125
+ }
126
+
127
+ }
128
+
80
129
  return x;
81
130
 
82
131
  } catch (e) {
package/src/wait.js ADDED
@@ -0,0 +1,3 @@
1
+ export default async function wait(ms) {
2
+ return new Promise(resolve => setTimeout(resolve, ms));
3
+ }
@@ -1,8 +1,10 @@
1
- export default function cleanString(x: any, { required, minLength, maxLength, allowHtml, allowScripts, validValues }?: {
1
+ export default function cleanString(x: any, { required, minLength, maxLength, allowHtml, allowScripts, validValues, format, pattern }?: {
2
2
  required?: boolean;
3
3
  minLength?: boolean;
4
4
  maxLength?: boolean;
5
5
  allowHtml?: boolean;
6
6
  allowScripts?: boolean;
7
7
  validValues?: boolean;
8
+ format?: boolean;
9
+ pattern?: boolean;
8
10
  }): string;
package/types/index.d.ts CHANGED
@@ -40,6 +40,7 @@ import type _utils_generateNamespaceCode from './utils/generateNamespaceCode';
40
40
  import type _utils_generateNamespaceExportLines from './utils/generateNamespaceExportLines';
41
41
  import type _uuid from './uuid';
42
42
  import type _validateSchema from './validateSchema';
43
+ import type _wait from './wait';
43
44
 
44
45
  export declare const build: typeof _build;
45
46
  export declare const collectImports: typeof _collectImports;
@@ -58,6 +59,7 @@ export declare const thingType: typeof _thingType;
58
59
  export declare const toPennies: typeof _toPennies;
59
60
  export declare const uuid: typeof _uuid;
60
61
  export declare const validateSchema: typeof _validateSchema;
62
+ export declare const wait: typeof _wait;
61
63
 
62
64
  export declare const clean: {
63
65
  array: typeof _clean_array;
@@ -135,6 +137,7 @@ declare const _default: {
135
137
  toPennies: typeof _toPennies;
136
138
  uuid: typeof _uuid;
137
139
  validateSchema: typeof _validateSchema;
140
+ wait: typeof _wait;
138
141
  clean: {
139
142
  array: typeof _clean_array,
140
143
  boolean: typeof _clean_boolean,
@@ -0,0 +1 @@
1
+ export default function wait(ms: any): Promise<any>;