@adobe/spacecat-shared-utils 1.0.1 → 1.1.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 CHANGED
@@ -1,3 +1,10 @@
1
+ # [@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)
2
+
3
+
4
+ ### Features
5
+
6
+ * 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))
7
+
1
8
  # [@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
9
 
3
10
 
package/README.md CHANGED
@@ -1,3 +1,57 @@
1
- # Spacecat Shared - Utils
1
+ # SpaceCat Shared Utilities
2
2
 
3
- A collection of utility functions.
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-utils",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Shared modules of the Spacecat Services - utils",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/functions.js CHANGED
@@ -49,21 +49,21 @@ function isNumber(value) {
49
49
  /**
50
50
  * Checks if the given parameter is an object and not an array or null.
51
51
  *
52
- * @param {*} obj - The object to check.
52
+ * @param {*} value - The value to check.
53
53
  * @returns {boolean} True if the parameter is an object, false otherwise.
54
54
  */
55
- function isObject(obj) {
56
- return !Array.isArray(obj) && obj !== null && typeof obj === 'object';
55
+ function isObject(value) {
56
+ return !Array.isArray(value) && value !== null && typeof value === 'object';
57
57
  }
58
58
 
59
59
  /**
60
60
  * Determines if the given parameter is a string.
61
61
  *
62
- * @param {*} str - The string to check.
62
+ * @param {*} value - The value to check.
63
63
  * @returns {boolean} True if the parameter is a string, false otherwise.
64
64
  */
65
- function isString(str) {
66
- return (!!str || str === '') && typeof str === 'string';
65
+ function isString(value) {
66
+ return (!!value || value === '') && typeof value === 'string';
67
67
  }
68
68
 
69
69
  /**
@@ -73,17 +73,17 @@ function isString(str) {
73
73
  * @returns {boolean} True if the string is not empty, false otherwise.
74
74
  */
75
75
  function hasText(str) {
76
- return !!str && typeof str === 'string';
76
+ return !!str && isString(str);
77
77
  }
78
78
 
79
79
  /**
80
80
  * Checks whether the given object is a valid JavaScript Date.
81
81
  *
82
- * @param {*} obj - The object to check.
82
+ * @param {*} value - The value to check.
83
83
  * @returns {boolean} True if the given object is a valid Date object, false otherwise.
84
84
  */
85
- function isValidDate(obj) {
86
- return obj instanceof Date && !Number.isNaN(obj.getTime());
85
+ function isValidDate(value) {
86
+ return value instanceof Date && !Number.isNaN(value.getTime());
87
87
  }
88
88
 
89
89
  /**
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;