@ezez/utils 1.1.0 → 1.4.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 (197) hide show
  1. package/.github/workflows/docs.yml +50 -0
  2. package/CHANGELOG.md +22 -0
  3. package/README.md +45 -20
  4. package/dist/ensureDate.d.ts +3 -0
  5. package/dist/ensureDate.d.ts.map +1 -0
  6. package/dist/ensureDate.js +11 -0
  7. package/dist/ensureDate.js.map +1 -0
  8. package/dist/ensureError.d.ts.map +1 -1
  9. package/dist/ensureError.js.map +1 -1
  10. package/dist/ensurePrefix.d.ts +3 -0
  11. package/dist/ensurePrefix.d.ts.map +1 -0
  12. package/dist/ensurePrefix.js +11 -0
  13. package/dist/ensurePrefix.js.map +1 -0
  14. package/dist/ensureSuffix.d.ts +3 -0
  15. package/dist/ensureSuffix.d.ts.map +1 -0
  16. package/dist/ensureSuffix.js +11 -0
  17. package/dist/ensureSuffix.js.map +1 -0
  18. package/dist/ensureTimestamp.d.ts +3 -0
  19. package/dist/ensureTimestamp.d.ts.map +1 -0
  20. package/dist/ensureTimestamp.js +11 -0
  21. package/dist/ensureTimestamp.js.map +1 -0
  22. package/dist/escapeRegExp.d.ts +3 -0
  23. package/dist/escapeRegExp.d.ts.map +1 -0
  24. package/dist/escapeRegExp.js +8 -0
  25. package/dist/escapeRegExp.js.map +1 -0
  26. package/dist/index.d.ts +10 -0
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +10 -0
  29. package/dist/index.js.map +1 -1
  30. package/dist/isPlainObject.d.ts.map +1 -1
  31. package/dist/isPlainObject.js.map +1 -1
  32. package/dist/replace.d.ts +3 -0
  33. package/dist/replace.d.ts.map +1 -0
  34. package/dist/replace.js +10 -0
  35. package/dist/replace.js.map +1 -0
  36. package/dist/safe.d.ts +4 -0
  37. package/dist/safe.d.ts.map +1 -0
  38. package/dist/safe.js +13 -0
  39. package/dist/safe.js.map +1 -0
  40. package/dist/sortProps.d.ts +3 -0
  41. package/dist/sortProps.d.ts.map +1 -0
  42. package/dist/sortProps.js +19 -0
  43. package/dist/sortProps.js.map +1 -0
  44. package/dist/stripPrefix.d.ts +3 -0
  45. package/dist/stripPrefix.d.ts.map +1 -0
  46. package/dist/stripPrefix.js +11 -0
  47. package/dist/stripPrefix.js.map +1 -0
  48. package/dist/stripSuffix.d.ts +3 -0
  49. package/dist/stripSuffix.d.ts.map +1 -0
  50. package/dist/stripSuffix.js +11 -0
  51. package/dist/stripSuffix.js.map +1 -0
  52. package/docs/assets/highlight.css +14 -0
  53. package/docs/assets/main.js +1 -1
  54. package/docs/assets/search.js +1 -1
  55. package/docs/assets/style.css +4 -5
  56. package/docs/functions/cap.html +17 -6
  57. package/docs/functions/capitalize.html +17 -6
  58. package/docs/functions/coalesce.html +19 -9
  59. package/docs/functions/ensureArray.html +18 -7
  60. package/docs/functions/ensureDate.html +131 -0
  61. package/docs/functions/ensureError.html +27 -6
  62. package/docs/functions/ensurePrefix.html +133 -0
  63. package/docs/functions/ensureSuffix.html +133 -0
  64. package/docs/functions/ensureTimestamp.html +131 -0
  65. package/docs/functions/escapeRegExp.html +127 -0
  66. package/docs/functions/get.html +17 -6
  67. package/docs/functions/getMultiple.html +17 -6
  68. package/docs/functions/insertSeparator.html +17 -6
  69. package/docs/functions/isEmpty.html +17 -6
  70. package/docs/functions/isNumericString.html +17 -6
  71. package/docs/functions/isPlainObject.html +32 -6
  72. package/docs/functions/last.html +17 -6
  73. package/docs/functions/mapAsync.html +17 -6
  74. package/docs/functions/mapValues.html +17 -6
  75. package/docs/functions/match.html +17 -6
  76. package/docs/functions/merge.html +25 -14
  77. package/docs/functions/mostFrequent.html +17 -6
  78. package/docs/functions/noop.html +17 -6
  79. package/docs/functions/occurrences.html +17 -6
  80. package/docs/functions/omit.html +17 -6
  81. package/docs/functions/pick.html +17 -6
  82. package/docs/functions/pull.html +17 -6
  83. package/docs/functions/remove.html +17 -6
  84. package/docs/functions/replace.html +133 -0
  85. package/docs/functions/rethrow.html +17 -6
  86. package/docs/functions/safe.html +171 -0
  87. package/docs/functions/scale.html +17 -6
  88. package/docs/functions/seq.html +17 -6
  89. package/docs/functions/seqEarlyBreak.html +17 -6
  90. package/docs/functions/set.html +17 -6
  91. package/docs/functions/setImmutable.html +17 -6
  92. package/docs/functions/sortBy.html +17 -6
  93. package/docs/functions/sortProps.html +136 -0
  94. package/docs/functions/stripPrefix.html +127 -0
  95. package/docs/functions/stripSuffix.html +127 -0
  96. package/docs/functions/throttle.html +17 -6
  97. package/docs/functions/truthy.html +17 -6
  98. package/docs/functions/wait.html +17 -6
  99. package/docs/functions/waitFor.html +17 -6
  100. package/docs/functions/waitSync.html +17 -6
  101. package/docs/index.html +92 -25
  102. package/docs/interfaces/GetMultipleSource.html +17 -6
  103. package/docs/interfaces/GetSource.html +17 -6
  104. package/docs/interfaces/IsNumericStringOptions.html +11 -10
  105. package/docs/interfaces/OccurencesOptions.html +8 -7
  106. package/docs/interfaces/SetImmutableSource.html +17 -6
  107. package/docs/interfaces/SetSource.html +17 -6
  108. package/docs/interfaces/ThrottleOptions.html +9 -8
  109. package/docs/interfaces/ThrottledFunctionExtras.html +9 -8
  110. package/docs/modules.html +26 -5
  111. package/docs/pages/CHANGELOG.html +764 -0
  112. package/docs/pages/Introduction.html +16 -5
  113. package/docs/types/MapValuesFn.html +17 -6
  114. package/docs/types/MatchCallback.html +17 -6
  115. package/docs/types/SeqEarlyBreaker.html +17 -6
  116. package/docs/types/SeqFn.html +17 -6
  117. package/docs/types/SeqFunctions.html +17 -6
  118. package/docs/types/SetImmutablePath.html +17 -6
  119. package/docs/types/ThrottledFunction.html +17 -6
  120. package/docs/variables/mapValuesUNSET.html +17 -6
  121. package/docs/variables/mergeUNSET.html +17 -6
  122. package/esm/ensureDate.d.ts +3 -0
  123. package/esm/ensureDate.d.ts.map +1 -0
  124. package/esm/ensureDate.js +8 -0
  125. package/esm/ensureDate.js.map +1 -0
  126. package/esm/ensureError.d.ts.map +1 -1
  127. package/esm/ensureError.js.map +1 -1
  128. package/esm/ensurePrefix.d.ts +3 -0
  129. package/esm/ensurePrefix.d.ts.map +1 -0
  130. package/esm/ensurePrefix.js +8 -0
  131. package/esm/ensurePrefix.js.map +1 -0
  132. package/esm/ensureSuffix.d.ts +3 -0
  133. package/esm/ensureSuffix.d.ts.map +1 -0
  134. package/esm/ensureSuffix.js +8 -0
  135. package/esm/ensureSuffix.js.map +1 -0
  136. package/esm/ensureTimestamp.d.ts +3 -0
  137. package/esm/ensureTimestamp.d.ts.map +1 -0
  138. package/esm/ensureTimestamp.js +8 -0
  139. package/esm/ensureTimestamp.js.map +1 -0
  140. package/esm/escapeRegExp.d.ts +3 -0
  141. package/esm/escapeRegExp.d.ts.map +1 -0
  142. package/esm/escapeRegExp.js +5 -0
  143. package/esm/escapeRegExp.js.map +1 -0
  144. package/esm/index.d.ts +10 -0
  145. package/esm/index.d.ts.map +1 -1
  146. package/esm/index.js +10 -0
  147. package/esm/index.js.map +1 -1
  148. package/esm/isPlainObject.d.ts.map +1 -1
  149. package/esm/isPlainObject.js.map +1 -1
  150. package/esm/replace.d.ts +3 -0
  151. package/esm/replace.d.ts.map +1 -0
  152. package/esm/replace.js +7 -0
  153. package/esm/replace.js.map +1 -0
  154. package/esm/safe.d.ts +4 -0
  155. package/esm/safe.d.ts.map +1 -0
  156. package/esm/safe.js +10 -0
  157. package/esm/safe.js.map +1 -0
  158. package/esm/sortProps.d.ts +3 -0
  159. package/esm/sortProps.d.ts.map +1 -0
  160. package/esm/sortProps.js +16 -0
  161. package/esm/sortProps.js.map +1 -0
  162. package/esm/stripPrefix.d.ts +3 -0
  163. package/esm/stripPrefix.d.ts.map +1 -0
  164. package/esm/stripPrefix.js +8 -0
  165. package/esm/stripPrefix.js.map +1 -0
  166. package/esm/stripSuffix.d.ts +3 -0
  167. package/esm/stripSuffix.d.ts.map +1 -0
  168. package/esm/stripSuffix.js +8 -0
  169. package/esm/stripSuffix.js.map +1 -0
  170. package/package.json +23 -23
  171. package/src/coalesce.ts +2 -2
  172. package/src/ensureArray.ts +1 -1
  173. package/src/ensureDate.spec.ts +13 -0
  174. package/src/ensureDate.ts +18 -0
  175. package/src/ensureError.ts +7 -0
  176. package/src/ensurePrefix.spec.ts +11 -0
  177. package/src/ensurePrefix.ts +18 -0
  178. package/src/ensureSuffix.spec.ts +11 -0
  179. package/src/ensureSuffix.ts +18 -0
  180. package/src/ensureTimestamp.spec.ts +11 -0
  181. package/src/ensureTimestamp.ts +18 -0
  182. package/src/escapeRegExp.spec.ts +31 -0
  183. package/src/escapeRegExp.ts +17 -0
  184. package/src/index.ts +10 -0
  185. package/src/isPlainObject.spec.ts +0 -7
  186. package/src/isPlainObject.ts +5 -0
  187. package/src/replace.spec.ts +22 -0
  188. package/src/replace.ts +19 -0
  189. package/src/safe.spec.ts +52 -0
  190. package/src/safe.ts +24 -0
  191. package/src/sortProps.spec.ts +20 -0
  192. package/src/sortProps.ts +21 -0
  193. package/src/stripPrefix.spec.ts +13 -0
  194. package/src/stripPrefix.ts +15 -0
  195. package/src/stripSuffix.spec.ts +13 -0
  196. package/src/stripSuffix.ts +16 -0
  197. package/typedoc.cjs +5 -0
@@ -22,13 +22,6 @@ describe("isPlainObject", function() {
22
22
  isPlainObject({ constructor: () => null }).must.be.true();
23
23
  });
24
24
 
25
- it("accepts plain objects", function() {
26
- isPlainObject({}).must.be.true();
27
- isPlainObject({ some: "data" }).must.be.true();
28
- isPlainObject({ some: function fn() {} }).must.be.true();
29
- isPlainObject(Object.create(null)).must.be.true();
30
- });
31
-
32
25
  it("shouldn't accept any instances", function() {
33
26
  // eslint-disable-next-line @typescript-eslint/no-extraneous-class
34
27
  class X {}
@@ -1,5 +1,10 @@
1
1
  /**
2
2
  * Checks if given value is a plain object. Plain object should be an object that's not an instance of anything.
3
+ * @example isPlainObject({}); // returns true
4
+ * @example isPlainObject(Object.create(null)); // returns true
5
+ * @example isPlainObject(new URL("https://ezez.dev")); // returns false
6
+ * @example isPlainObject([]); // returns false
7
+ * @example isPlainObject(5); // returns false
3
8
  * @param value - value to test
4
9
  */
5
10
  const isPlainObject = (value: unknown) => Boolean(
@@ -0,0 +1,22 @@
1
+ import { replace } from "./replace.js";
2
+
3
+ describe("replace", () => {
4
+ it("does a basic replace", () => {
5
+ replace("Hello, %name%!", { "%name%": "John" }).must.equal("Hello, John!");
6
+ });
7
+
8
+ it("replaces multiple occurrences", () => {
9
+ replace("Hello, %name%! Nice to meet you %name%!", { "%name%": "Jane" })
10
+ .must.equal("Hello, Jane! Nice to meet you Jane!");
11
+ });
12
+
13
+ it("replaces multiple occurences of multiple variables", () => {
14
+ replace("Hello, %name%, your age is %age%! Nice to meet you %age% yo %name%!",
15
+ { "%name%": "Jane", "%age%": "30" },
16
+ ).must.equal("Hello, Jane, your age is 30! Nice to meet you 30 yo Jane!");
17
+ });
18
+
19
+ it("doesn't break on regexp characters", () => {
20
+ replace("Hello.", { ".": "!" }).must.equal("Hello!");
21
+ });
22
+ });
package/src/replace.ts ADDED
@@ -0,0 +1,19 @@
1
+ /* eslint-disable max-len */
2
+ import { escapeRegExp } from "./escapeRegExp.js";
3
+
4
+ /**
5
+ * Replaces all occurrences of the keys in the replaceMap with the values.
6
+ * @param source - source string
7
+ * @param replaceMap - keys from this object will be replaced with values in source string
8
+ * @example replace("Hello, %name%!", { "%name%: "John" }) // "Hello, John!"
9
+ * @example replace("Hello, %name%! Nice to meet you %name%!", { "%name%": "Jane" }) // "Hello, Jane! Nice to meet you Jane!"
10
+ */
11
+ const replace = (source: string, replaceMap: Record<string, string>) => {
12
+ /* eslint-enable max-len */
13
+ const regex = new RegExp(Object.keys(replaceMap).map(escapeRegExp).join("|"), "g");
14
+ return source.replace(regex, (matched) => replaceMap[matched]);
15
+ };
16
+
17
+ export {
18
+ replace,
19
+ };
@@ -0,0 +1,52 @@
1
+ import { safe } from "./safe.js";
2
+
3
+ describe("safe", () => {
4
+ it("returns value from a function if it doesn't throw", () => {
5
+ const result = safe(() => 5);
6
+ result.must.equal(5);
7
+ });
8
+
9
+ it("returns undefined if function throws and no default value is given", () => {
10
+ // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
11
+ const result = safe(() => {
12
+ throw new Error("Boo");
13
+ });
14
+ (result === undefined).must.be.true();
15
+ });
16
+
17
+ it("returns given default value if function throws", () => {
18
+ const result = safe(() => {
19
+ throw new Error("Boo");
20
+ }, 15);
21
+ result.must.equal(15);
22
+ });
23
+
24
+ it("returns undefined if function throws and default value is undefined", () => {
25
+ // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
26
+ const result = safe(() => {
27
+ throw new Error("Boo");
28
+ }, undefined);
29
+ (result === undefined).must.be.true();
30
+ });
31
+
32
+ it("returns value from function if it doesn't throw and default is given", () => {
33
+ const result = safe(() => 5, 15);
34
+ result.must.equal(5);
35
+ });
36
+
37
+ it("doesn't unwrap promises", () => {
38
+ const result = safe(() => Promise.resolve(5));
39
+ result.must.be.instanceof(Promise);
40
+ });
41
+
42
+ it("doesn't 'work' with promises", () => {
43
+ // eslint-disable-next-line @typescript-eslint/require-await
44
+ const result = safe(async () => {
45
+ throw new Error("Boo!!!");
46
+ });
47
+ result.must.be.instanceof(Promise);
48
+
49
+ // prevent unhandled rejection warning
50
+ result.catch(() => null);
51
+ });
52
+ });
package/src/safe.ts ADDED
@@ -0,0 +1,24 @@
1
+ function safe<T>(fn: () => T): T | undefined;
2
+ function safe<T, Y>(fn: () => T, def: Y): T | Y;
3
+
4
+ /* eslint-disable max-len */
5
+ /**
6
+ * Safely execute a function, return its return value or default value if the function throws.
7
+ * @param fn - function to run
8
+ * @param def - default value
9
+ *
10
+ * @example
11
+ * safe(() => JSON.parse(unknownString), null); // if unknownString is not a valid JSON, null will be returned
12
+ * safe(() => trySomethingComplicated(), defaultValue); // if trySomethingComplicated throws, defaultValue will be returned
13
+ */
14
+ function safe<T, Y>(fn: () => T, def?: Y) { // eslint-disable-line func-style
15
+ /* eslint-enable max-len */
16
+ try {
17
+ return fn();
18
+ }
19
+ catch {
20
+ return def;
21
+ }
22
+ }
23
+
24
+ export { safe };
@@ -0,0 +1,20 @@
1
+ import { sortProps } from "./sortProps.js";
2
+
3
+ describe("sort props", () => {
4
+ it("can sort props ascending", () => {
5
+ const sorted = sortProps({ b: 2, a: 1, z: 26 });
6
+ Object.keys(sorted).must.eql(["a", "b", "z"]);
7
+ });
8
+
9
+ it("can sort props descending", () => {
10
+ const sorted = sortProps({ b: 2, a: 1, z: 26 }, false);
11
+ Object.keys(sorted).must.eql(["z", "b", "a"]);
12
+ });
13
+
14
+ it("doesn't modify original object", () => {
15
+ const source = { b: 2, a: 1, z: 26 };
16
+ const sorted = sortProps(source);
17
+ Object.keys(source).must.eql(["b", "a", "z"]);
18
+ Object.keys(sorted).must.eql(["a", "b", "z"]);
19
+ });
20
+ });
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Sorts the properties of an object alphabetically, ascending or descending.
3
+ * REMEMBER: In theory JS engines do not guarantee the order of object properties. In practice most popular engines do.
4
+ * @param object - source object
5
+ * @param asc - sort ascending?
6
+ * @example sortProps({ b: 2, a: 1, z: 26 }) // { a: 1, b: 2, z: 26 }
7
+ */
8
+ const sortProps = <T extends Record<string, unknown>>(object: T, asc = true): T => {
9
+ const sorted: Record<string, unknown> = {};
10
+ const keys = Object.keys(object);
11
+ if (asc) { keys.sort(); }
12
+ else { keys.sort().reverse(); }
13
+ for (const key of keys) {
14
+ sorted[key] = object[key];
15
+ }
16
+ return sorted as T;
17
+ };
18
+
19
+ export {
20
+ sortProps,
21
+ };
@@ -0,0 +1,13 @@
1
+ import { stripPrefix } from "./stripPrefix.js";
2
+
3
+ describe("strip prefix", function() {
4
+ it("should strip prefix if string has it", function() {
5
+ stripPrefix("abc", "a").must.equal("bc");
6
+ stripPrefix("hello world", "hello").must.equal(" world");
7
+ });
8
+
9
+ it("should keep the string as it is when string don't have specified prefix", function() {
10
+ stripPrefix("abc", "b").must.equal("abc");
11
+ stripPrefix("hello world", "world").must.equal("hello world");
12
+ });
13
+ });
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Strip a prefix from a string.
3
+ * @param {string} from - string to strip prefix from
4
+ * @param {string} prefix - prefix to strip
5
+ */
6
+ const stripPrefix = (from: string, prefix: string): string => {
7
+ if (from.startsWith(prefix)) {
8
+ return from.slice(prefix.length);
9
+ }
10
+ return from;
11
+ };
12
+
13
+ export {
14
+ stripPrefix,
15
+ };
@@ -0,0 +1,13 @@
1
+ import { stripSuffix } from "./stripSuffix.js";
2
+
3
+ describe("strip suffix", function() {
4
+ it("should strip suffix if string has it", function() {
5
+ stripSuffix("abc", "c").must.equal("ab");
6
+ stripSuffix("hello world", "world").must.equal("hello ");
7
+ });
8
+
9
+ it("should keep the string as it is when string don't have specified suffix", function() {
10
+ stripSuffix("abc", "b").must.equal("abc");
11
+ stripSuffix("hello world", "hello").must.equal("hello world");
12
+ });
13
+ });
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Strip a suffix from a string.
3
+ *
4
+ * @param {string} from - string to strip suffix from
5
+ * @param {string} suffix - suffix to strip
6
+ */
7
+ const stripSuffix = (from: string, suffix: string): string => {
8
+ if (from.endsWith(suffix)) {
9
+ return from.slice(0, -suffix.length);
10
+ }
11
+ return from;
12
+ };
13
+
14
+ export {
15
+ stripSuffix,
16
+ };
package/typedoc.cjs ADDED
@@ -0,0 +1,5 @@
1
+ const json = require("./pagesconfig.json");
2
+
3
+ module.exports = {
4
+ pluginPages: json,
5
+ };