@adobe/spacecat-shared-utils 1.0.1 → 1.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.
- package/CHANGELOG.md +14 -0
- package/README.md +56 -2
- package/package.json +1 -1
- package/src/functions.js +23 -12
- package/src/index.d.ts +35 -0
- package/src/index.js +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
# [@adobe/spacecat-shared-utils-v1.2.0](https://github.com/adobe-rnd/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.1.0...@adobe/spacecat-shared-utils-v1.2.0) (2023-11-30)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* add isArray function ([eeb45a6](https://github.com/adobe-rnd/spacecat-shared/commit/eeb45a6784c6406403d0a59b65ba873a107c654c))
|
|
7
|
+
|
|
8
|
+
# [@adobe/spacecat-shared-utils-v1.1.0](https://github.com/adobe-rnd/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.0.1...@adobe/spacecat-shared-utils-v1.1.0) (2023-11-29)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* add guards and use spacecat-shared-utils ([#6](https://github.com/adobe-rnd/spacecat-shared/issues/6)) ([27143cf](https://github.com/adobe-rnd/spacecat-shared/commit/27143cf2d4a439f1b5904e62756e59e501b3f67d))
|
|
14
|
+
|
|
1
15
|
# [@adobe/spacecat-shared-utils-v1.0.1](https://github.com/adobe-rnd/spacecat-shared/compare/@adobe/spacecat-shared-utils-v1.0.0...@adobe/spacecat-shared-utils-v1.0.1) (2023-11-28)
|
|
2
16
|
|
|
3
17
|
|
package/README.md
CHANGED
|
@@ -1,3 +1,57 @@
|
|
|
1
|
-
#
|
|
1
|
+
# SpaceCat Shared Utilities
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
This repository contains a collection of shared utility functions used across various SpaceCat projects. These utilities provide a range of checks and validations, from basic data type validation to more complex checks like ISO date strings and URL validation.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
To install the SpaceCat Shared Utilities, you can use npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install spacecat-shared-utils
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or, if you are using yarn:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add spacecat-shared-utils
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
Here's how you can use the different utility functions in your project:
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
import { isBoolean, isValidUrl } from 'spacecat-shared-utils';
|
|
25
|
+
|
|
26
|
+
console.log(isBoolean('true')); // true
|
|
27
|
+
console.log(isValidUrl('https://www.example.com')); // true
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Functions
|
|
31
|
+
|
|
32
|
+
The library includes the following utility functions:
|
|
33
|
+
|
|
34
|
+
- `isBoolean(value)`: Determines if the given value is a boolean or a string representation of a boolean.
|
|
35
|
+
- `isInteger(value)`: Checks if the given value is an integer.
|
|
36
|
+
- `isValidDate(obj)`: Checks whether the given object is a valid JavaScript Date.
|
|
37
|
+
- `isIsoDate(str)`: Validates whether the given string is a JavaScript ISO date string in Zulu (UTC) timezone.
|
|
38
|
+
- `isIsoTimeOffsetsDate(str)`: Validates whether the given string is a JavaScript ISO date string following UTC time offsets format.
|
|
39
|
+
- `isNumber(value)`: Determines if the given value is a number.
|
|
40
|
+
- `isObject(obj)`: Checks if the given parameter is an object and not an array or null.
|
|
41
|
+
- `isString(str)`: Determines if the given parameter is a string.
|
|
42
|
+
- `toBoolean(value)`: Converts a given value to a boolean. Throws an error if the value is not a boolean.
|
|
43
|
+
- `arrayEquals(a, b)`: Compares two arrays for equality.
|
|
44
|
+
- `isValidUrl(urlString)`: Validates whether the given string is a valid URL with http or https protocol.
|
|
45
|
+
- `hasText(str)`: Checks if the given string is not empty.
|
|
46
|
+
|
|
47
|
+
## Testing
|
|
48
|
+
|
|
49
|
+
This library includes a comprehensive test suite to ensure the reliability of the utility functions. To run the tests, use the following command:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
npm test
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE.txt) file for details.
|
package/package.json
CHANGED
package/src/functions.js
CHANGED
|
@@ -14,6 +14,16 @@
|
|
|
14
14
|
const REGEX_ISO_DATE = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/;
|
|
15
15
|
const REGEX_TIME_OFFSET_DATE = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}(Z|[+-]\d{2}:\d{2})/;
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Determines if the given parameter is an array.
|
|
19
|
+
*
|
|
20
|
+
* @param {*} value - The value to check.
|
|
21
|
+
* @returns {boolean} True if the parameter is an array, false otherwise.
|
|
22
|
+
*/
|
|
23
|
+
function isArray(value) {
|
|
24
|
+
return Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
17
27
|
/**
|
|
18
28
|
* Determines case-insensitively if the given value is a boolean or a string
|
|
19
29
|
* representation of a boolean.
|
|
@@ -49,21 +59,21 @@ function isNumber(value) {
|
|
|
49
59
|
/**
|
|
50
60
|
* Checks if the given parameter is an object and not an array or null.
|
|
51
61
|
*
|
|
52
|
-
* @param {*}
|
|
62
|
+
* @param {*} value - The value to check.
|
|
53
63
|
* @returns {boolean} True if the parameter is an object, false otherwise.
|
|
54
64
|
*/
|
|
55
|
-
function isObject(
|
|
56
|
-
return !
|
|
65
|
+
function isObject(value) {
|
|
66
|
+
return !isArray(value) && value !== null && typeof value === 'object';
|
|
57
67
|
}
|
|
58
68
|
|
|
59
69
|
/**
|
|
60
70
|
* Determines if the given parameter is a string.
|
|
61
71
|
*
|
|
62
|
-
* @param {*}
|
|
72
|
+
* @param {*} value - The value to check.
|
|
63
73
|
* @returns {boolean} True if the parameter is a string, false otherwise.
|
|
64
74
|
*/
|
|
65
|
-
function isString(
|
|
66
|
-
return (!!
|
|
75
|
+
function isString(value) {
|
|
76
|
+
return (!!value || value === '') && typeof value === 'string';
|
|
67
77
|
}
|
|
68
78
|
|
|
69
79
|
/**
|
|
@@ -73,17 +83,17 @@ function isString(str) {
|
|
|
73
83
|
* @returns {boolean} True if the string is not empty, false otherwise.
|
|
74
84
|
*/
|
|
75
85
|
function hasText(str) {
|
|
76
|
-
return !!str &&
|
|
86
|
+
return !!str && isString(str);
|
|
77
87
|
}
|
|
78
88
|
|
|
79
89
|
/**
|
|
80
90
|
* Checks whether the given object is a valid JavaScript Date.
|
|
81
91
|
*
|
|
82
|
-
* @param {*}
|
|
92
|
+
* @param {*} value - The value to check.
|
|
83
93
|
* @returns {boolean} True if the given object is a valid Date object, false otherwise.
|
|
84
94
|
*/
|
|
85
|
-
function isValidDate(
|
|
86
|
-
return
|
|
95
|
+
function isValidDate(value) {
|
|
96
|
+
return value instanceof Date && !Number.isNaN(value.getTime());
|
|
87
97
|
}
|
|
88
98
|
|
|
89
99
|
/**
|
|
@@ -150,14 +160,15 @@ function toBoolean(value) {
|
|
|
150
160
|
* @param {Array} b - The second array to compare.
|
|
151
161
|
* @returns {boolean} True if the arrays are equal, false otherwise.
|
|
152
162
|
*/
|
|
153
|
-
const arrayEquals = (a, b) =>
|
|
154
|
-
&&
|
|
163
|
+
const arrayEquals = (a, b) => isArray(a)
|
|
164
|
+
&& isArray(b)
|
|
155
165
|
&& a.length === b.length
|
|
156
166
|
&& a.every((val, index) => val === b[index]);
|
|
157
167
|
|
|
158
168
|
export {
|
|
159
169
|
arrayEquals,
|
|
160
170
|
hasText,
|
|
171
|
+
isArray,
|
|
161
172
|
isBoolean,
|
|
162
173
|
isInteger,
|
|
163
174
|
isValidDate,
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export function arrayEquals<T>(a: T[], b: T[]): boolean;
|
|
14
|
+
|
|
15
|
+
export function hasText(str: string): boolean;
|
|
16
|
+
|
|
17
|
+
export function isBoolean(value: unknown): boolean;
|
|
18
|
+
|
|
19
|
+
export function isInteger(value: unknown): boolean;
|
|
20
|
+
|
|
21
|
+
export function isValidDate(value: unknown): boolean;
|
|
22
|
+
|
|
23
|
+
export function isIsoDate(str: string): boolean;
|
|
24
|
+
|
|
25
|
+
export function isIsoTimeOffsetsDate(str: string): boolean;
|
|
26
|
+
|
|
27
|
+
export function isNumber(value: unknown): boolean;
|
|
28
|
+
|
|
29
|
+
export function isObject(value: unknown): boolean;
|
|
30
|
+
|
|
31
|
+
export function isString(value: unknown): boolean;
|
|
32
|
+
|
|
33
|
+
export function toBoolean(value: unknown): boolean;
|
|
34
|
+
|
|
35
|
+
export function isValidUrl(urlString: string): boolean;
|