@elyukai/utils 0.2.4 → 0.2.8

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
@@ -2,6 +2,25 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
4
4
 
5
+ ## [0.2.8](https://github.com/elyukai/ts-utils/compare/v0.2.7...v0.2.8) (2026-02-24)
6
+
7
+ ## [0.2.7](https://github.com/elyukai/ts-utils/compare/v0.2.6...v0.2.7) (2026-02-24)
8
+
9
+ ## [0.2.6](https://github.com/elyukai/ts-utils/compare/v0.2.5...v0.2.6) (2026-02-24)
10
+
11
+
12
+ ### Features
13
+
14
+ * add static traverse method to Reader ([693840d](https://github.com/elyukai/ts-utils/commit/693840dde90a0f4d650b0e075610e21a6b1a4318))
15
+ * safe non-empty array map method call ([5a97a69](https://github.com/elyukai/ts-utils/commit/5a97a69f25ac8431d3bc29405b312f750f0e30b9))
16
+
17
+ ## [0.2.5](https://github.com/elyukai/ts-utils/compare/v0.2.4...v0.2.5) (2026-02-22)
18
+
19
+
20
+ ### Features
21
+
22
+ * add allSame array function ([8cac98b](https://github.com/elyukai/ts-utils/commit/8cac98bd18e3a4ac7f7acb95b57e55ea031630b4))
23
+
5
24
  ## [0.2.4](https://github.com/elyukai/ts-utils/compare/v0.2.3...v0.2.4) (2026-02-20)
6
25
 
7
26
  ## [0.2.3](https://github.com/elyukai/ts-utils/compare/v0.2.2...v0.2.3) (2026-02-20)
@@ -16,3 +16,11 @@ export declare const anySame: <T>(arr: T[], equalityCheck?: (a: T, b: T) => bool
16
16
  * of found duplicates where the values are the indices of these values.
17
17
  */
18
18
  export declare const anySameIndices: <T>(arr: T[], equalityCheck?: (a: T, b: T) => boolean) => number[][];
19
+ /**
20
+ * Checks if all elements in the array are the same. An empty array is considered to satisfy this condition.
21
+ *
22
+ * @param arr The array to check.
23
+ * @param equalityCheck An optional function to determine if two elements are considered the same. Defaults to strict shallow equality.
24
+ * @returns `true` if all elements in the array are the same, otherwise `false`.
25
+ */
26
+ export declare const allSame: <T>(arr: T[], equalityCheck?: (a: T, b: T) => boolean) => boolean;
@@ -2,6 +2,7 @@
2
2
  * Utility functions for filtering arrays.
3
3
  * @module
4
4
  */
5
+ import { isNotEmpty } from "./nonEmpty.js";
5
6
  /**
6
7
  * Filters out duplicate values from an array. Objects are not supported, since
7
8
  * they don’t provide value equality semantics.
@@ -26,3 +27,11 @@ export const anySameIndices = (arr, equalityCheck = (a, b) => a === b) => arr.re
26
27
  : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- The index must exist according to the findIndex above
27
28
  acc.with(accIndex, [...acc[accIndex], index]);
28
29
  }, []);
30
+ /**
31
+ * Checks if all elements in the array are the same. An empty array is considered to satisfy this condition.
32
+ *
33
+ * @param arr The array to check.
34
+ * @param equalityCheck An optional function to determine if two elements are considered the same. Defaults to strict shallow equality.
35
+ * @returns `true` if all elements in the array are the same, otherwise `false`.
36
+ */
37
+ export const allSame = (arr, equalityCheck = (a, b) => a === b) => isNotEmpty(arr) ? arr.every((item) => equalityCheck(item, arr[0])) : true;
package/dist/reader.d.ts CHANGED
@@ -50,6 +50,10 @@ export declare class Reader<in R, out T> {
50
50
  * Computes the result by providing the required environment.
51
51
  */
52
52
  run(env: R): T;
53
+ /**
54
+ * Apply a function to each value in the array, and return a Reader that produces an array of the results.
55
+ */
56
+ static traverse<T, R, U>(values: T[], fn: (value: T) => Reader<R, U>): Reader<R, U[]>;
53
57
  /**
54
58
  * Evaluate each reader in the array, and collect the results in a single reader instance.
55
59
  */
package/dist/reader.js CHANGED
@@ -2,6 +2,7 @@
2
2
  * A simple implementation of the Reader monad, which allows you to write functions that depend on some shared environment.
3
3
  * @module
4
4
  */
5
+ import { identity } from "./function.js";
5
6
  // A simpler wrapper for working with text that is generated based on different contexts, which encapsulates the context and reduces main code clutter. The context is only applied in the end.
6
7
  /**
7
8
  * The Reader monad represents a computation that depends on some shared environment. It allows you to write functions that can access this environment without having to pass it explicitly as an argument.
@@ -71,11 +72,17 @@ export class Reader {
71
72
  run(env) {
72
73
  return this.#fn(env);
73
74
  }
75
+ /**
76
+ * Apply a function to each value in the array, and return a Reader that produces an array of the results.
77
+ */
78
+ static traverse(values, fn) {
79
+ return new Reader((env) => values.map((value) => fn(value).run(env)));
80
+ }
74
81
  /**
75
82
  * Evaluate each reader in the array, and collect the results in a single reader instance.
76
83
  */
77
84
  static sequence(readers) {
78
- return new Reader((env) => readers.map((reader) => reader.run(env)));
85
+ return Reader.traverse(readers, identity);
79
86
  }
80
87
  /**
81
88
  * Transforms a function that returns a Reader into a Reader that produces a function.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elyukai/utils",
3
- "version": "0.2.4",
3
+ "version": "0.2.8",
4
4
  "description": "A set of JavaScript helper functions.",
5
5
  "files": [
6
6
  "dist",