mybase 1.1.2 → 1.1.3

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/mybase.js CHANGED
@@ -7,7 +7,6 @@ const net = require('net')
7
7
  const chalk = require('chalk')
8
8
  const punycode = require('punycode');
9
9
  const _validURL = require('@7c/validurl')
10
- const { match } = require('assert');
11
10
  const validator = require('validator')
12
11
  const sha512 = require('js-sha512')
13
12
  const ip6addr = require('ip6addr')
@@ -178,7 +177,7 @@ function validURL(host) {
178
177
  return _validURL(host)
179
178
  }
180
179
 
181
- function asJSON(str) {
180
+ function asJSON(str) { // ported
182
181
  if (!str || typeof str!=='string') return false
183
182
  try { return JSON.parse(str)}catch(_) {}
184
183
  return false
@@ -574,7 +573,7 @@ function validUUID4(uuid) {
574
573
  return new RegExp(/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i).test(uuid)
575
574
  }
576
575
 
577
- function promiseTimeout(ms, promise) {
576
+ function promiseTimeout(ms, promise) { // ported
578
577
  // Create a promise that rejects in <ms> milliseconds
579
578
  let timeout = new Promise((resolve, reject) => {
580
579
  let id = setTimeout(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -0,0 +1,72 @@
1
+ const { asJSON } = require('./asJSON')
2
+
3
+ describe('asJSON', () => {
4
+ // Returns parsed JSON object when valid JSON string is passed as input.
5
+ it('should return parsed JSON object when valid JSON string is passed as input', () => {
6
+ const inputString = '{"name": "John", "age": 30}';
7
+ const expectedOutput = { name: "John", age: 30 };
8
+ const result = asJSON(inputString);
9
+ expect(result).toEqual(expectedOutput);
10
+ });
11
+
12
+ // Returns false when empty string is passed as input.
13
+ it('should return false when empty string is passed as input', () => {
14
+ const inputString = "";
15
+ const expectedOutput = false;
16
+ const result = asJSON(inputString);
17
+ expect(result).toEqual(expectedOutput);
18
+ });
19
+
20
+ // Returns false when non-string input is passed.
21
+ it('should return false when non-string input is passed', () => {
22
+ const inputString = 123;
23
+ const expectedOutput = false;
24
+ const result = asJSON(inputString);
25
+ expect(result).toEqual(expectedOutput);
26
+ });
27
+
28
+ // Returns false when null is passed as input.
29
+ it('should return false when null is passed as input', () => {
30
+ const inputString = null;
31
+ const expectedOutput = false;
32
+ const result = asJSON(inputString);
33
+ expect(result).toEqual(expectedOutput);
34
+ });
35
+
36
+ // Returns false when undefined is passed as input.
37
+ it('should return false when undefined is passed as input', () => {
38
+ const inputString = undefined;
39
+ const expectedOutput = false;
40
+ const result = asJSON(inputString);
41
+ expect(result).toEqual(expectedOutput);
42
+ });
43
+
44
+ // Returns false when NaN is passed as input.
45
+ it('should return false when NaN is passed as input', () => {
46
+ const inputString = NaN;
47
+ const expectedOutput = false;
48
+ const result = asJSON(inputString);
49
+ expect(result).toEqual(expectedOutput);
50
+ });
51
+
52
+ // Returns false when invalid JSON string is passed as input.
53
+ it('should return false when invalid JSON string is passed as input', () => {
54
+ const inputString = 'invalid json';
55
+ const result = asJSON(inputString);
56
+ expect(result).toBe(false);
57
+ });
58
+
59
+ // Returns false when Infinity is passed as input.
60
+ it('should return false when Infinity is passed as input', () => {
61
+ const inputString = 'Infinity';
62
+ const result = asJSON(inputString);
63
+ expect(result).toBe(false);
64
+ });
65
+
66
+ // Returns false when input string contains a single quote.
67
+ it('should return false when input string contains a single quote', () => {
68
+ const inputString = "{'name': 'John'}";
69
+ const result = asJSON(inputString);
70
+ expect(result).toBe(false);
71
+ });
72
+ });
@@ -0,0 +1,6 @@
1
+
2
+ export function asJSON(inputString: string): any | boolean {
3
+ if (!inputString || typeof inputString !== 'string') return false
4
+ try { return JSON.parse(inputString) } catch (_) { }
5
+ return false
6
+ }
@@ -0,0 +1,104 @@
1
+ const { promiseTimeout } = require('./promiseTimeout');
2
+
3
+ describe('promiseTimeout', () => {
4
+
5
+ // Returns a promise that resolves when inputPromise resolves before the timeout.
6
+ it('should resolve when inputPromise resolves before the timeout', () => {
7
+ // Arrange
8
+ const inputPromise = new Promise((resolve) => {
9
+ setTimeout(() => {
10
+ resolve('Resolved');
11
+ }, 100);
12
+ });
13
+
14
+ // Act
15
+ const resultPromise = promiseTimeout(200, inputPromise);
16
+
17
+ // Assert
18
+ return expect(resultPromise).resolves.toBe('Resolved');
19
+ });
20
+
21
+ // Returns a promise that rejects with onTimeout when inputPromise rejects after the timeout.
22
+ it('should reject with onTimeout when inputPromise rejects after the timeout', () => {
23
+ // Arrange
24
+ const inputPromise = new Promise((resolve, reject) => {
25
+ setTimeout(() => {
26
+ reject('Rejected');
27
+ }, 200);
28
+ });
29
+
30
+ // Act
31
+ const resultPromise = promiseTimeout(100, inputPromise);
32
+
33
+ // Assert
34
+ return expect(resultPromise).rejects.toBe('TIMEDOUT');
35
+ });
36
+
37
+ // Returns a promise that rejects with onTimeout when inputPromise is pending after the timeout.
38
+ it('should reject with onTimeout when inputPromise is pending after the timeout', () => {
39
+ // Arrange
40
+ const inputPromise = new Promise(() => { });
41
+
42
+ // Act
43
+ const resultPromise = promiseTimeout(100, inputPromise);
44
+
45
+ // Assert
46
+ return expect(resultPromise).rejects.toBe('TIMEDOUT');
47
+ });
48
+
49
+ // Returns a promise that resolves with inputPromise value when inputPromise resolves before the timeout.
50
+ it('should resolve with inputPromise value when inputPromise resolves before the timeout', () => {
51
+ // Arrange
52
+ const inputPromise = new Promise((resolve) => {
53
+ setTimeout(() => {
54
+ resolve('Resolved');
55
+ }, 100);
56
+ });
57
+
58
+ // Act
59
+ const resultPromise = promiseTimeout(200, inputPromise);
60
+
61
+ // Assert
62
+ return expect(resultPromise).resolves.toBe('Resolved');
63
+ });
64
+
65
+ // Returns a promise that rejects with inputPromise value when inputPromise rejects before the timeout.
66
+ it('should reject with inputPromise value when inputPromise rejects before the timeout', () => {
67
+ // Arrange
68
+ const inputPromise = new Promise((resolve, reject) => {
69
+ setTimeout(() => {
70
+ reject('Rejected');
71
+ }, 100);
72
+ });
73
+
74
+ // Act
75
+ const resultPromise = promiseTimeout(200, inputPromise);
76
+
77
+ // Assert
78
+ return expect(resultPromise).rejects.toBe('Rejected');
79
+ });
80
+
81
+ // Returns a promise that rejects with inputPromise value when inputPromise is pending before the timeout.
82
+ it('should reject with inputPromise value when inputPromise is pending before the timeout', () => {
83
+ // Arrange
84
+ const inputPromise = new Promise(() => { });
85
+
86
+ // Act
87
+ const resultPromise = promiseTimeout(200, inputPromise);
88
+
89
+ // Assert
90
+ return expect(resultPromise).rejects.toBe('TIMEDOUT');
91
+ });
92
+
93
+ // Returns a promise that resolves with undefined when inputPromise is undefined.
94
+ it('should resolve with undefined when inputPromise is undefined', () => {
95
+ // Arrange
96
+ const inputPromise = undefined;
97
+
98
+ // Act
99
+ const resultPromise = promiseTimeout(200, inputPromise);
100
+
101
+ // Assert
102
+ return expect(resultPromise).resolves.toBe(undefined);
103
+ });
104
+ });
@@ -0,0 +1,19 @@
1
+
2
+
3
+
4
+
5
+ export function promiseTimeout(miliseconds:number, inputPromise:Promise<any>, onTimeout:any='TIMEDOUT') { // ported
6
+ // Create a promise that rejects in <miliseconds> milliseconds
7
+ let timeout = new Promise((resolve, reject) => {
8
+ let stID = setTimeout(() => {
9
+ clearTimeout(stID);
10
+ reject(onTimeout)
11
+ }, miliseconds)
12
+ })
13
+
14
+ // Returns a race between our timeout and the passed in promise
15
+ return Promise.race([
16
+ inputPromise,
17
+ timeout
18
+ ])
19
+ }
package/ts/index.test.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as IMPORTS from "./index";
2
2
 
3
3
  test('exports', () => {
4
- for(let key in IMPORTS) {
5
- expect(typeof IMPORTS[key]).toBe('function')
4
+ for(let fn of Object.values(IMPORTS)) {
5
+ expect(typeof fn).toBe('function')
6
6
  }
7
7
  })
package/ts/index.ts CHANGED
@@ -8,3 +8,5 @@ export * from "./funcs/fileCacheIsValid"
8
8
  export * from "./funcs/hash_sha512"
9
9
  export * from "./funcs/vaultRead"
10
10
  export * from "./funcs/vaultFill"
11
+ export * from "./funcs/asJSON"
12
+ export * from "./funcs/promiseTimeout"