xjs-common 12.0.0-alpha.1 → 12.0.0-alpha.2

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.
@@ -1,7 +1,7 @@
1
1
  export type IndexSignature = string | number | symbol;
2
2
  export type NormalRecord<T = any> = Record<IndexSignature, T>;
3
3
  export type MaybeArray<T = any> = T | T[];
4
- export type MaybePromise<T = void> = T | Promise<T>;
4
+ export type MaybePromise<T = any> = T | Promise<T>;
5
5
  export type Loggable = {
6
6
  log: (msg: any) => void;
7
7
  warn: (msg: any) => void;
@@ -14,6 +14,42 @@ export type IdName<T = number> = {
14
14
  name: string;
15
15
  } & Unique<T>;
16
16
  export type Any = string | number | bigint | boolean | symbol | object | undefined | null;
17
+ export interface AlmostArray<E = any> extends ArrayLike<E> {
18
+ /** {@link Array.copyWithin()} */
19
+ copyWithin(target: number, start: number, end?: number): this;
20
+ /** {@link Array.every()} */
21
+ every(predicate: (value: E, index: number, array: this) => unknown, thisArg?: any): boolean;
22
+ /** {@link Array.fill()} */
23
+ fill(value: E, start?: number, end?: number): this;
24
+ /** {@link Array.filter()} */
25
+ filter(predicate: (value: E, index: number, array: this) => any, thisArg?: any): this;
26
+ /** {@link Array.find()} */
27
+ find(predicate: (value: E, index: number, obj: this) => boolean, thisArg?: any): E | undefined;
28
+ /** {@link Array.findIndex()} */
29
+ findIndex(predicate: (value: E, index: number, obj: this) => boolean, thisArg?: any): number;
30
+ /** {@link Array.forEach()} */
31
+ forEach(callbackfn: (value: E, index: number, array: this) => void, thisArg?: any): void;
32
+ /** {@link Array.indexOf()} */
33
+ indexOf(searchElement: E, fromIndex?: number): number;
34
+ /** {@link Array.join()} */
35
+ join(separator?: string): string;
36
+ /** {@link Array.lastIndexOf()} */
37
+ lastIndexOf(searchElement: E, fromIndex?: number): number;
38
+ /** {@link Array.map()} */
39
+ map(callbackfn: (value: E, index: number, array: this) => number, thisArg?: any): this;
40
+ /** {@link Array.reduce()} */
41
+ reduce<U = E>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: this) => U, initialValue?: U): U;
42
+ /** {@link Array.reduceRight()} */
43
+ reduceRight<U = E>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: this) => U, initialValue?: U): U;
44
+ /** {@link Array.reverse()} */
45
+ reverse(): this;
46
+ /** {@link Array.slice()} */
47
+ slice(start?: number, end?: number): this;
48
+ /** {@link Array.some()} */
49
+ some(predicate: (value: E, index: number, array: this) => unknown, thisArg?: any): boolean;
50
+ /** {@link Array.sort()} */
51
+ sort(compareFn?: (a: E, b: E) => number): this;
52
+ }
17
53
  export declare enum Type {
18
54
  string = "string",
19
55
  number = "number",
@@ -1,24 +1,18 @@
1
+ import { AlmostArray } from "../const/types";
1
2
  export declare namespace UArray {
2
3
  /**
3
4
  * compares two arrays to valuate equality.
4
5
  * if one side is null or undefined, it returns true when other side is the same.
6
+ * this function has compatibility to `AlmostArray` like `Uint8Array` etc.
5
7
  * @param v1 it uses equal operator for comparing elements, so applying object element is not recommended.
6
8
  * @param v2 same as v1.
7
- * @param sort it uses {@link Array#sort} on v1 and v2 if true. default is true.
8
- * @param useStrictEqual it uses `===` operator for compareing elements if true, otherwise using `==` operator. default is true.
9
+ * @param sort it uses {@link Array.sort()} on v1 and v2 if true. default is true.
10
+ * @param useStrictEqual check equality whether `Array` or not and it uses `===` operator for compareing elements if true, otherwise using `==` operator. default is true.
9
11
  */
10
- function eq(v1: any[], v2: any[], op: {
12
+ function eq(v1: any[] | AlmostArray, v2: any[] | AlmostArray, op?: {
11
13
  sort?: boolean;
12
- useStrictEqual: false;
14
+ useStrictEqual?: boolean;
13
15
  }): boolean;
14
- function eq<T>(v1: T[], v2: T[], op: {
15
- sort?: boolean;
16
- useStrictEqual: true;
17
- }): boolean;
18
- function eq<T>(v1: T[], v2: T[], op: {
19
- sort?: boolean;
20
- }): boolean;
21
- function eq<T>(v1: T[], v2: T[]): boolean;
22
16
  /**
23
17
  * returns array which is removed duplicate of elements.
24
18
  * this doesn't mutate the param.
@@ -32,7 +26,12 @@ export declare namespace UArray {
32
26
  predicate: (v1: T, v2: T) => boolean;
33
27
  takeLast?: boolean;
34
28
  }): T[];
35
- function chop<T>(array: T[], len: number): T[][];
29
+ /**
30
+ * chop array to partial arrays which have specified length. the remainder is added to end of a result.
31
+ * this function has compatibility to `AlmostArray` like `Uint8Array` etc.
32
+ */
33
+ function chop<E>(array: E[], len: number): E[][];
34
+ function chop<T extends AlmostArray>(array: T, len: number): T[];
36
35
  function remove<T>(array: T[], v: T): void;
37
36
  function randomPick<T>(array: T[], takeout?: boolean): T;
38
37
  function shuffle<T>(array: T[]): T[];
@@ -1,6 +1,15 @@
1
1
  import { array2map, int2array } from "./u";
2
2
  export var UArray;
3
3
  (function (UArray) {
4
+ /**
5
+ * compares two arrays to valuate equality.
6
+ * if one side is null or undefined, it returns true when other side is the same.
7
+ * this function has compatibility to `AlmostArray` like `Uint8Array` etc.
8
+ * @param v1 it uses equal operator for comparing elements, so applying object element is not recommended.
9
+ * @param v2 same as v1.
10
+ * @param sort it uses {@link Array.sort()} on v1 and v2 if true. default is true.
11
+ * @param useStrictEqual check equality whether `Array` or not and it uses `===` operator for compareing elements if true, otherwise using `==` operator. default is true.
12
+ */
4
13
  function eq(v1, v2, op = {}) {
5
14
  const { sort, useStrictEqual } = Object.assign({ sort: true, useStrictEqual: true }, op);
6
15
  if (v1 && !v2 || !v1 && v2)
@@ -9,7 +18,9 @@ export var UArray;
9
18
  return true;
10
19
  if (v1.length !== v2.length)
11
20
  return false;
12
- const a = sort ? [...v1].sort() : v1, b = sort ? [...v2].sort() : v2;
21
+ if (useStrictEqual && v1 instanceof Array !== v2 instanceof Array)
22
+ return false;
23
+ const a = sort ? v1.slice().sort() : v1, b = sort ? v2.slice().sort() : v2;
13
24
  return a.every((v, i) => useStrictEqual ? v === b[i] : v == b[i]);
14
25
  }
15
26
  UArray.eq = eq;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-common",
3
- "version": "12.0.0-alpha.1",
3
+ "version": "12.0.0-alpha.2",
4
4
  "description": "library modules for typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",