@ezez/utils 1.1.0 → 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.
Files changed (134) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +41 -20
  3. package/dist/ensureDate.d.ts +3 -0
  4. package/dist/ensureDate.d.ts.map +1 -0
  5. package/dist/ensureDate.js +11 -0
  6. package/dist/ensureDate.js.map +1 -0
  7. package/dist/ensurePrefix.d.ts +3 -0
  8. package/dist/ensurePrefix.d.ts.map +1 -0
  9. package/dist/ensurePrefix.js +11 -0
  10. package/dist/ensurePrefix.js.map +1 -0
  11. package/dist/ensureSuffix.d.ts +3 -0
  12. package/dist/ensureSuffix.d.ts.map +1 -0
  13. package/dist/ensureSuffix.js +11 -0
  14. package/dist/ensureSuffix.js.map +1 -0
  15. package/dist/ensureTimestamp.d.ts +3 -0
  16. package/dist/ensureTimestamp.d.ts.map +1 -0
  17. package/dist/ensureTimestamp.js +11 -0
  18. package/dist/ensureTimestamp.js.map +1 -0
  19. package/dist/index.d.ts +6 -0
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +6 -0
  22. package/dist/index.js.map +1 -1
  23. package/dist/stripPrefix.d.ts +3 -0
  24. package/dist/stripPrefix.d.ts.map +1 -0
  25. package/dist/stripPrefix.js +11 -0
  26. package/dist/stripPrefix.js.map +1 -0
  27. package/dist/stripSuffix.d.ts +3 -0
  28. package/dist/stripSuffix.d.ts.map +1 -0
  29. package/dist/stripSuffix.js +11 -0
  30. package/dist/stripSuffix.js.map +1 -0
  31. package/docs/assets/search.js +1 -1
  32. package/docs/functions/cap.html +11 -5
  33. package/docs/functions/capitalize.html +11 -5
  34. package/docs/functions/coalesce.html +11 -5
  35. package/docs/functions/ensureArray.html +11 -5
  36. package/docs/functions/ensureDate.html +126 -0
  37. package/docs/functions/ensureError.html +11 -5
  38. package/docs/functions/ensurePrefix.html +128 -0
  39. package/docs/functions/ensureSuffix.html +128 -0
  40. package/docs/functions/ensureTimestamp.html +126 -0
  41. package/docs/functions/get.html +11 -5
  42. package/docs/functions/getMultiple.html +11 -5
  43. package/docs/functions/insertSeparator.html +11 -5
  44. package/docs/functions/isEmpty.html +11 -5
  45. package/docs/functions/isNumericString.html +11 -5
  46. package/docs/functions/isPlainObject.html +11 -5
  47. package/docs/functions/last.html +11 -5
  48. package/docs/functions/mapAsync.html +11 -5
  49. package/docs/functions/mapValues.html +11 -5
  50. package/docs/functions/match.html +11 -5
  51. package/docs/functions/merge.html +19 -13
  52. package/docs/functions/mostFrequent.html +11 -5
  53. package/docs/functions/noop.html +11 -5
  54. package/docs/functions/occurrences.html +11 -5
  55. package/docs/functions/omit.html +11 -5
  56. package/docs/functions/pick.html +11 -5
  57. package/docs/functions/pull.html +11 -5
  58. package/docs/functions/remove.html +11 -5
  59. package/docs/functions/rethrow.html +11 -5
  60. package/docs/functions/scale.html +11 -5
  61. package/docs/functions/seq.html +11 -5
  62. package/docs/functions/seqEarlyBreak.html +11 -5
  63. package/docs/functions/set.html +11 -5
  64. package/docs/functions/setImmutable.html +11 -5
  65. package/docs/functions/sortBy.html +11 -5
  66. package/docs/functions/stripPrefix.html +122 -0
  67. package/docs/functions/stripSuffix.html +122 -0
  68. package/docs/functions/throttle.html +11 -5
  69. package/docs/functions/truthy.html +11 -5
  70. package/docs/functions/wait.html +11 -5
  71. package/docs/functions/waitFor.html +11 -5
  72. package/docs/functions/waitSync.html +11 -5
  73. package/docs/index.html +82 -24
  74. package/docs/interfaces/GetMultipleSource.html +11 -5
  75. package/docs/interfaces/GetSource.html +11 -5
  76. package/docs/interfaces/IsNumericStringOptions.html +9 -9
  77. package/docs/interfaces/OccurencesOptions.html +6 -6
  78. package/docs/interfaces/SetImmutableSource.html +11 -5
  79. package/docs/interfaces/SetSource.html +11 -5
  80. package/docs/interfaces/ThrottleOptions.html +7 -7
  81. package/docs/interfaces/ThrottledFunctionExtras.html +7 -7
  82. package/docs/modules.html +16 -4
  83. package/docs/pages/Introduction.html +10 -4
  84. package/docs/types/MapValuesFn.html +11 -5
  85. package/docs/types/MatchCallback.html +11 -5
  86. package/docs/types/SeqEarlyBreaker.html +11 -5
  87. package/docs/types/SeqFn.html +11 -5
  88. package/docs/types/SeqFunctions.html +11 -5
  89. package/docs/types/SetImmutablePath.html +11 -5
  90. package/docs/types/ThrottledFunction.html +11 -5
  91. package/docs/variables/mapValuesUNSET.html +11 -5
  92. package/docs/variables/mergeUNSET.html +11 -5
  93. package/esm/ensureDate.d.ts +3 -0
  94. package/esm/ensureDate.d.ts.map +1 -0
  95. package/esm/ensureDate.js +8 -0
  96. package/esm/ensureDate.js.map +1 -0
  97. package/esm/ensurePrefix.d.ts +3 -0
  98. package/esm/ensurePrefix.d.ts.map +1 -0
  99. package/esm/ensurePrefix.js +8 -0
  100. package/esm/ensurePrefix.js.map +1 -0
  101. package/esm/ensureSuffix.d.ts +3 -0
  102. package/esm/ensureSuffix.d.ts.map +1 -0
  103. package/esm/ensureSuffix.js +8 -0
  104. package/esm/ensureSuffix.js.map +1 -0
  105. package/esm/ensureTimestamp.d.ts +3 -0
  106. package/esm/ensureTimestamp.d.ts.map +1 -0
  107. package/esm/ensureTimestamp.js +8 -0
  108. package/esm/ensureTimestamp.js.map +1 -0
  109. package/esm/index.d.ts +6 -0
  110. package/esm/index.d.ts.map +1 -1
  111. package/esm/index.js +6 -0
  112. package/esm/index.js.map +1 -1
  113. package/esm/stripPrefix.d.ts +3 -0
  114. package/esm/stripPrefix.d.ts.map +1 -0
  115. package/esm/stripPrefix.js +8 -0
  116. package/esm/stripPrefix.js.map +1 -0
  117. package/esm/stripSuffix.d.ts +3 -0
  118. package/esm/stripSuffix.d.ts.map +1 -0
  119. package/esm/stripSuffix.js +8 -0
  120. package/esm/stripSuffix.js.map +1 -0
  121. package/package.json +1 -1
  122. package/src/ensureDate.spec.ts +13 -0
  123. package/src/ensureDate.ts +18 -0
  124. package/src/ensurePrefix.spec.ts +11 -0
  125. package/src/ensurePrefix.ts +18 -0
  126. package/src/ensureSuffix.spec.ts +11 -0
  127. package/src/ensureSuffix.ts +18 -0
  128. package/src/ensureTimestamp.spec.ts +11 -0
  129. package/src/ensureTimestamp.ts +18 -0
  130. package/src/index.ts +6 -0
  131. package/src/stripPrefix.spec.ts +13 -0
  132. package/src/stripPrefix.ts +15 -0
  133. package/src/stripSuffix.spec.ts +13 -0
  134. package/src/stripSuffix.ts +16 -0
@@ -0,0 +1,8 @@
1
+ const stripPrefix = (from, prefix) => {
2
+ if (from.startsWith(prefix)) {
3
+ return from.slice(prefix.length);
4
+ }
5
+ return from;
6
+ };
7
+ export { stripPrefix, };
8
+ //# sourceMappingURL=stripPrefix.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stripPrefix.js","sourceRoot":"","sources":["../src/stripPrefix.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,GAAG,CAAC,IAAY,EAAE,MAAc,EAAU,EAAE;IACzD,IAAI,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE;QACzB,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;KACpC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC,CAAC;AAEF,OAAO,EACH,WAAW,GACd,CAAC"}
@@ -0,0 +1,3 @@
1
+ declare const stripSuffix: (from: string, suffix: string) => string;
2
+ export { stripSuffix, };
3
+ //# sourceMappingURL=stripSuffix.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stripSuffix.d.ts","sourceRoot":"","sources":["../src/stripSuffix.ts"],"names":[],"mappings":"AAMA,QAAA,MAAM,WAAW,SAAU,MAAM,UAAU,MAAM,KAAG,MAKnD,CAAC;AAEF,OAAO,EACH,WAAW,GACd,CAAC"}
@@ -0,0 +1,8 @@
1
+ const stripSuffix = (from, suffix) => {
2
+ if (from.endsWith(suffix)) {
3
+ return from.slice(0, -suffix.length);
4
+ }
5
+ return from;
6
+ };
7
+ export { stripSuffix, };
8
+ //# sourceMappingURL=stripSuffix.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stripSuffix.js","sourceRoot":"","sources":["../src/stripSuffix.ts"],"names":[],"mappings":"AAMA,MAAM,WAAW,GAAG,CAAC,IAAY,EAAE,MAAc,EAAU,EAAE;IACzD,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;QACvB,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;KACxC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC,CAAC;AAEF,OAAO,EACH,WAAW,GACd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ezez/utils",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "repository": "https://github.com/dzek69/bottom-line.git",
5
5
  "author": "Jacek Nowacki @dzek69 <git-public@dzek.eu>",
6
6
  "license": "MIT",
@@ -0,0 +1,13 @@
1
+ import { ensureDate } from "./ensureDate.js";
2
+
3
+ describe("ensureDate", function() {
4
+ it("should convert timestamps to date instances", function() {
5
+ ensureDate(123456789).must.be.instanceOf(Date);
6
+ ensureDate(123456789).getTime().must.equal(123456789);
7
+ });
8
+
9
+ it("should return the same date instance", function() {
10
+ const date = new Date(123456789);
11
+ ensureDate(date).must.equal(date);
12
+ });
13
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Ensure that the date value is a Date instance
3
+ *
4
+ * @example ensureDate(123456789); // Date(123456789)
5
+ * @example ensureDate(new Date(123456789)); // Date(123456789) -- same instance as given
6
+ * @see {@link ensureTimestamp}
7
+ * @param {Date | number} date - date value
8
+ */
9
+ const ensureDate = (date: Date | number): Date => {
10
+ if (typeof date === "number") {
11
+ return new Date(date);
12
+ }
13
+ return date;
14
+ };
15
+
16
+ export {
17
+ ensureDate,
18
+ };
@@ -0,0 +1,11 @@
1
+ import { ensurePrefix } from "./ensurePrefix.js";
2
+
3
+ describe("ensure prefix", function() {
4
+ it("adds prefix", function() {
5
+ ensurePrefix("1234bbcc", "0x").must.equal("0x1234bbcc");
6
+ });
7
+
8
+ it("doesn't add prefix if already present", function() {
9
+ ensurePrefix("0x1234bbcc", "0x").must.equal("0x1234bbcc");
10
+ });
11
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Add prefix to string if it doesn't already have it
3
+ *
4
+ * @example ensurePrefix("1234bbcc", "0x") -> "0x1234bbcc"
5
+ * @example ensurePrefix("0x1234bbcc", "0x") -> "0x1234bbcc"
6
+ * @param {string} string - string to add prefix to
7
+ * @param {string} prefix - prefix to add
8
+ */
9
+ const ensurePrefix = (string: string, prefix: string) => {
10
+ if (string.startsWith(prefix)) {
11
+ return string;
12
+ }
13
+ return prefix + string;
14
+ };
15
+
16
+ export {
17
+ ensurePrefix,
18
+ };
@@ -0,0 +1,11 @@
1
+ import { ensureSuffix } from "./ensureSuffix.js";
2
+
3
+ describe("ensure suffix", function() {
4
+ it("adds suffix to a string", function() {
5
+ ensureSuffix("hello world", ".").must.equal("hello world.");
6
+ });
7
+
8
+ it("doesn't add suffix if already present", function() {
9
+ ensureSuffix("hello world.", ".").must.equal("hello world.");
10
+ });
11
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Add suffix to string if it does not already end with it
3
+ *
4
+ * @example ensureSuffix("hello world", ".") -> "hello world."
5
+ * @example ensureSuffix("hello world.", ".") -> "hello world."
6
+ * @param {string} string - string to add suffix to
7
+ * @param {string} suffix - suffix to add
8
+ */
9
+ const ensureSuffix = (string: string, suffix: string) => {
10
+ if (string.endsWith(suffix)) {
11
+ return string;
12
+ }
13
+ return string + suffix;
14
+ };
15
+
16
+ export {
17
+ ensureSuffix,
18
+ };
@@ -0,0 +1,11 @@
1
+ import { ensureTimestamp } from "./ensureTimestamp.js";
2
+
3
+ describe("ensure timestamp", function() {
4
+ it("returns number if number given", function() {
5
+ ensureTimestamp(123456789).must.equal(123456789);
6
+ });
7
+
8
+ it("returns number if date given", function() {
9
+ ensureTimestamp(new Date(123456789)).must.equal(123456789);
10
+ });
11
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Ensures that the given date value is a numeric timestamp.
3
+ *
4
+ * @example ensureTimestamp(123456789); // 123456789
5
+ * @example ensureTimestamp(new Date(123456789)); // 123456789
6
+ * @see {@link ensureDate}
7
+ * @param {Date | number} date - date value
8
+ */
9
+ const ensureTimestamp = (date: Date | number): number => {
10
+ if (typeof date === "number") {
11
+ return date;
12
+ }
13
+ return date.getTime();
14
+ };
15
+
16
+ export {
17
+ ensureTimestamp,
18
+ };
package/src/index.ts CHANGED
@@ -1,9 +1,13 @@
1
1
  /* eslint-disable import/max-dependencies */
2
+ export * from "./ensurePrefix.js";
3
+ export * from "./ensureSuffix.js";
2
4
  export * from "./cap.js";
3
5
  export * from "./capitalize.js";
4
6
  export * from "./coalesce.js";
5
7
  export * from "./ensureArray.js";
8
+ export * from "./ensureDate.js";
6
9
  export * from "./ensureError.js";
10
+ export * from "./ensureTimestamp.js";
7
11
  export * from "./get.js";
8
12
  export * from "./getMultiple.js";
9
13
  export * from "./insertSeparator.js";
@@ -28,6 +32,8 @@ export * from "./seq.js";
28
32
  export * from "./set.js";
29
33
  export * from "./setImmutable.js";
30
34
  export * from "./sortBy.js";
35
+ export * from "./stripPrefix.js";
36
+ export * from "./stripSuffix.js";
31
37
  export * from "./throttle.js";
32
38
  export * from "./truthy.js";
33
39
  export * from "./wait.js";
@@ -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
+ };