diff --git a/docs/.vitepress/items/general.ts b/docs/.vitepress/items/general.ts index 7d7f80f..c8121bd 100644 --- a/docs/.vitepress/items/general.ts +++ b/docs/.vitepress/items/general.ts @@ -9,6 +9,7 @@ export const generalItems = [ { text: 'isArray', link: '/general/is-array' }, { text: 'isNullish', link: '/general/is-nullish' }, { text: 'isPromise', link: '/general/is-promise' }, + { text: 'isRegExp', link: '/general/is-reg-exp' }, { text: 'isFunction', link: '/general/is-function' }, { text: 'isDate', link: '/general/is-date' }, { text: 'isSet', link: '/general/is-set' }, @@ -19,9 +20,12 @@ export const generalItems = [ { text: 'isArrayBuffer', link: '/general/is-array-buffer' }, { text: 'isTypedArray', link: '/general/is-typed-array' }, { text: 'isDataView', link: '/general/is-data-view' }, + { text: 'isError', link: '/general/is-error' }, + { text: 'isDOMException', link: '/general/is-dom-exception' }, { text: 'isWindow', link: '/general/is-window' }, - { text: 'isRegExp', link: '/general/is-reg-exp' }, { text: 'isEmpty', link: '/general/is-empty' }, + { text: 'isEqual', link: '/general/is-equal' }, + { text: 'isEqualWith', link: '/general/is-equal-with' }, { text: 'isNonEmptyArray', link: '/general/is-non-empty-array' }, { text: 'inBrowser', link: '/general/in-browser' }, { text: 'inMobile', link: '/general/in-mobile' }, diff --git a/docs/general/is-dom-exception.md b/docs/general/is-dom-exception.md new file mode 100644 index 0000000..f8f765d --- /dev/null +++ b/docs/general/is-dom-exception.md @@ -0,0 +1,24 @@ +# isDOMException + +Determine whether the input value is a `DOMException` object. + +### Usage + +```ts +import { isDOMException } from 'rattail' + +isDOMException(new DOMException('An error occurred')) // return true +isDOMException(new Error('An error occurred')) // return false +``` + +### Arguments + +| Arg | Type | Defaults | +| ----- | :---: | -------: | +| `val` | `any` | | + +### Return + +| Type | +| :-------: | +| `boolean` | diff --git a/docs/general/is-equal-with.md b/docs/general/is-equal-with.md new file mode 100644 index 0000000..d3ebac4 --- /dev/null +++ b/docs/general/is-equal-with.md @@ -0,0 +1,30 @@ +# isEqualWith + +Deeply compare two values. Supports passing a comparison method, and returns `true` if the two values ​​are equal. + +### Usage + +```ts +import { isEqualWith, isObject } from 'rattail' + +isEqualWith([1, 2], ['1', '2'], (v1, v2) => { + if (!isObject(v1) && !isObject(v2)) { + return String(v1) === String(v2) + } +}) +// return true +``` + +### Arguments + +| Arg | Type | Defaults | +| ------- | :---------------------: | -------: | +| `value` | `any` | | +| `other` | `any` | | +| `fn` | `(value, other) => any` | | + +### Return + +| Type | +| :-------: | +| `boolean` | diff --git a/docs/general/is-equal.md b/docs/general/is-equal.md new file mode 100644 index 0000000..9c67ecf --- /dev/null +++ b/docs/general/is-equal.md @@ -0,0 +1,27 @@ +# isEqual + +Deeply compare two values. + +### Usage + +```ts +import { isEqual } from 'rattail' + +isEqual({ n: 1 }, { n: 1 }) +// return true +isEqual([1, 2, 3], [1, 2, 3]) +// return true +``` + +### Arguments + +| Arg | Type | Defaults | +| ------- | :---: | -------: | +| `value` | `any` | | +| `other` | `any` | | + +### Return + +| Type | +| :-------: | +| `boolean` | diff --git a/docs/general/is-error.md b/docs/general/is-error.md new file mode 100644 index 0000000..d35391c --- /dev/null +++ b/docs/general/is-error.md @@ -0,0 +1,23 @@ +# isError + +Determine whether the input value is an `Error` object. + +### Usage + +```ts +import { isError } from 'rattail' + +isError(new Error('message')) // return true +``` + +### Arguments + +| Arg | Type | Defaults | +| ----- | :---: | -------: | +| `val` | `any` | | + +### Return + +| Type | +| :-------: | +| `boolean` | diff --git a/docs/zh/general/is-dom-exception.md b/docs/zh/general/is-dom-exception.md new file mode 100644 index 0000000..a756efd --- /dev/null +++ b/docs/zh/general/is-dom-exception.md @@ -0,0 +1,24 @@ +# isDOMException + +判断输入值是否为 `DOMException` 对象。 + +### 使用 + +```ts +import { isDOMException } from 'rattail' + +isDOMException(new DOMException('An error occurred')) // return true +isDOMException(new Error('An error occurred')) // return false +``` + +### 参数列表 + +| 参数 | 类型 | 默认值 | +| ----- | :---: | -----: | +| `val` | `any` | | + +### 返回值 + +| 类型 | +| :-------: | +| `boolean` | diff --git a/docs/zh/general/is-equal-with.md b/docs/zh/general/is-equal-with.md new file mode 100644 index 0000000..044c6c6 --- /dev/null +++ b/docs/zh/general/is-equal-with.md @@ -0,0 +1,30 @@ +# isEqualWith + +深度比较两个值。支持传入一个比较方法,返回 `true` 时表示两个值相等。 + +### 使用 + +```ts +import { isEqualWith, isObject } from 'rattail' + +isEqualWith([1, 2], ['1', '2'], (v1, v2) => { + if (!isObject(v1) && !isObject(v2)) { + return String(v1) === String(v2) + } +}) +// return true +``` + +### 参数列表 + +| 参数 | 类型 | 默认值 | +| ------- | :---------------------: | -----: | +| `value` | `any` | | +| `other` | `any` | | +| `fn` | `(value, other) => any` | | + +### 返回值 + +| 类型 | +| :-------: | +| `boolean` | diff --git a/docs/zh/general/is-equal.md b/docs/zh/general/is-equal.md new file mode 100644 index 0000000..c3e2314 --- /dev/null +++ b/docs/zh/general/is-equal.md @@ -0,0 +1,27 @@ +# isEqual + +深度比较两个值。 + +### 使用 + +```ts +import { isEqual } from 'rattail' + +isEqual({ n: 1 }, { n: 1 }) +// return true +isEqual([1, 2, 3], [1, 2, 3]) +// return true +``` + +### 参数列表 + +| 参数 | 类型 | 默认值 | +| ------- | :---: | -----: | +| `value` | `any` | | +| `other` | `any` | | + +### 返回值 + +| 类型 | +| :-------: | +| `boolean` | diff --git a/docs/zh/general/is-error.md b/docs/zh/general/is-error.md new file mode 100644 index 0000000..ff22ba8 --- /dev/null +++ b/docs/zh/general/is-error.md @@ -0,0 +1,23 @@ +# isError + +判断输入值是否为 `Error` 对象。 + +### 使用 + +```ts +import { isError } from 'rattail' + +isError(new Error('message')) // return true +``` + +### 参数列表 + +| 参数 | 类型 | 默认值 | +| ----- | :---: | -----: | +| `val` | `any` | | + +### 返回值 + +| 类型 | +| :-------: | +| `boolean` | diff --git a/tests/general.spec.ts b/tests/general.spec.ts index 4a839a8..a05e95e 100644 --- a/tests/general.spec.ts +++ b/tests/general.spec.ts @@ -363,6 +363,8 @@ it('isEqual', () => { expect(isEqual(new TextEncoder().encode('123').buffer, new TextEncoder().encode('1234').buffer)).toBe(false) expect(isEqual({ n: 1 }, { n: 1 })).toBe(true) + expect(isEqual([1, 2, 3], [1, 2, 3])).toBe(true) + expect(isEqual([1, 2, 3], { 0: 1, 1: 2, 2: 3 })).toBe(false) expect(isEqual({ a: 1, b: 2 }, { b: 2, a: 1 })).toBe(true) expect(isEqual({ n: 1 }, { n: 2 })).toBe(false) expect(isEqual({ n: 1, x: [1] }, { n: 1, x: [1] })).toBe(true) @@ -379,8 +381,14 @@ it('isEqual', () => { const c = [a, b] const d = [b, a] + const x: Record = { n: 1 } + const y: Record = { n: 1 } + x.self = x + y.self = y + expect(isEqual(a, b)).toBe(true) expect(isEqual(c, d)).toBe(true) + expect(isEqual(x, y)).toBe(true) }) it('isEqualWith', () => { @@ -393,4 +401,12 @@ it('isEqualWith', () => { (a, b) => isFunction(a) === isFunction(b), ), ).toBe(true) + + expect( + isEqualWith([1, 2], ['1', '2'], (v1, v2) => { + if (!isObject(v1) && !isObject(v2)) { + return String(v1) === String(v2) + } + }), + ).toBe(true) })