ts-type-predicates 1.0.8 → 1.0.10

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
@@ -3,6 +3,41 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.0.10](https://github.com/bluelovers/ws-ts-type/compare/ts-type-predicates@1.0.9...ts-type-predicates@1.0.10) (2026-03-07)
7
+
8
+
9
+
10
+ ### 📚 Documentation
11
+
12
+ * enhance documentation and JSDoc comments across multiple packages ([88ee99b](https://github.com/bluelovers/ws-ts-type/commit/88ee99b3a489645ca53093357cc3523dbe7996e0))
13
+
14
+
15
+ ### 🛠 Build System
16
+
17
+ * 更新多個套件的 test 指令為 jest ([61ea53b](https://github.com/bluelovers/ws-ts-type/commit/61ea53bf15fc3ed0e216793200604ae5a52079c9))
18
+
19
+
20
+ ### ♻️ Chores
21
+
22
+ * migrate from yarn to pnpm and enhance test infrastructure ([8a5daa2](https://github.com/bluelovers/ws-ts-type/commit/8a5daa2f2022eaf025c3349d4fe5dc8971f8c077))
23
+
24
+
25
+
26
+ ## [1.0.9](https://github.com/bluelovers/ws-ts-type/compare/ts-type-predicates@1.0.8...ts-type-predicates@1.0.9) (2022-10-10)
27
+
28
+
29
+
30
+ ### 📌 Dependencies
31
+
32
+ * update deps ([ab5f3e4](https://github.com/bluelovers/ws-ts-type/commit/ab5f3e48da1f10ddd4445ba6dda9a0a68c5f656f))
33
+
34
+
35
+ ### 🔖 Miscellaneous
36
+
37
+ * . ([9219017](https://github.com/bluelovers/ws-ts-type/commit/92190178fcf30f881c1d0d36fb141a3394ab3b38))
38
+
39
+
40
+
6
41
  ## [1.0.8](https://github.com/bluelovers/ws-ts-type/compare/ts-type-predicates@1.0.7...ts-type-predicates@1.0.8) (2022-08-10)
7
42
 
8
43
 
package/README.md CHANGED
@@ -1,8 +1,19 @@
1
- # README.md
1
+ # ts-type-predicates
2
2
 
3
- use asserts for make type predicates work
3
+ 使用斷言(assert)讓類型斷言(type predicates)運作的實用工具函式
4
4
 
5
- ## install
5
+ Use asserts to make type predicates work with better runtime validation
6
+
7
+ ## 功能特點 / Features
8
+
9
+ - 結合 TypeScript 斷言函式與類型斷言功能
10
+ - Combine TypeScript assertion functions with type predicate functionality
11
+ - 支援運行時驗證與編譯時類型縮小
12
+ - Support runtime validation and compile-time type narrowing
13
+ - 可自訂錯誤訊息
14
+ - Customizable error messages
15
+
16
+ ## 安裝 / Install
6
17
 
7
18
  ```bash
8
19
  yarn add ts-type-predicates
@@ -10,3 +21,23 @@ yarn-tool add ts-type-predicates
10
21
  yt add ts-type-predicates
11
22
  ```
12
23
 
24
+ ## 使用範例 / Usage Example
25
+
26
+ ```typescript
27
+ import { typePredicates, typeNarrowed } from 'ts-type-predicates';
28
+
29
+ // 使用斷言函式 / Using assertion function
30
+ function processValue(value: string | number) {
31
+ typePredicates<string>(value, typeof value === 'string');
32
+ // 現在 TypeScript 知道 value 是 string
33
+ console.log(value.toUpperCase());
34
+ }
35
+
36
+ // 使用類型收窄 / Using type narrowing
37
+ function checkValue(value: unknown) {
38
+ if (typeNarrowed<string>(value, typeof value === 'string')) {
39
+ console.log(value.length);
40
+ }
41
+ }
42
+ ```
43
+
package/dist/index.cjs CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
2
  'use strict'
3
3
 
4
- if (typeof process !== 'undefined' && process.env.NODE_ENV === 'production') {
5
- module.exports = require('./index.cjs.production.min.cjs')
6
- } else {
4
+ if (typeof process !== 'undefined' && process.env.NODE_ENV !== 'production') {
7
5
  module.exports = require('./index.cjs.development.cjs')
6
+ } else {
7
+ module.exports = require('./index.cjs.production.min.cjs')
8
8
  }
@@ -4,40 +4,69 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var assert = require('assert');
6
6
 
7
- function typePredicates(actual, expression = true, message, ignoreExpression) {
8
- var _expression;
9
-
10
- (_expression = expression) !== null && _expression !== void 0 ? _expression : expression = true;
11
-
7
+ /**
8
+ * 處理錶達式,回傳布林值結果
9
+ * Handle expression and return boolean result
10
+ *
11
+ * @param actual - 實際值 / Actual value
12
+ * @param expression - 錶達式,可為布林值或函式 / Expression, can be boolean or function
13
+ * @returns 布林值結果 / Boolean result
14
+ */
15
+ function _handleExpression(actual, expression = true) {
16
+ expression !== null && expression !== void 0 ? expression : expression = true;
12
17
  if (typeof expression === 'function') {
13
18
  expression = !!expression(actual);
14
19
  }
15
-
20
+ return expression;
21
+ }
22
+ /**
23
+ * 使用斷言(assert)讓類型斷言(type predicates)運作
24
+ * Use asserts for make type predicates work
25
+ *
26
+ * 此函式結合了 TypeScript 的斷言函式與類型斷言功能,
27
+ * 允許在運行時驗證類型並在編譯時縮小類型範圍
28
+ *
29
+ * @param actual - 實際值 / Actual value
30
+ * @param expression - 斷言條件,可為布林值或函式 / Assertion condition, can be boolean or function
31
+ * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)
32
+ * @param ignoreExpression - 是否忽略錶達式結果(可選)/ Whether to ignore expression result (optional)
33
+ * @throws 當錶達式結果為 false 時拋出 AssertionError / Throws AssertionError when expression is false
34
+ * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates
35
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
36
+ */
37
+ function typePredicates(actual, expression = true, message, ignoreExpression) {
38
+ expression = _handleExpression(actual, expression);
16
39
  if (expression !== true && ignoreExpression !== true) {
17
40
  throw new assert.AssertionError({
18
41
  message: message !== null && message !== void 0 ? message : `actual ${actual} not as expected`,
19
42
  actual,
20
- expected: expression
43
+ expected: expression,
44
+ operator: 'typePredicates'
21
45
  });
22
46
  }
23
47
  }
48
+ /**
49
+ * 類型收窄函式,回傳類型斷言結果
50
+ * Type narrowing function, returns type predicate result
51
+ *
52
+ * 此函式不會拋出錯誤,而是回傳布林值表示是否符合預期類型
53
+ * 可用於需要條件邏輯而非斷言的場景
54
+ *
55
+ * @param actual - 實際值 / Actual value
56
+ * @param expression - 驗證條件,可為布林值或函式 / Validation condition, can be boolean or function
57
+ * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)
58
+ * @returns 是否符合預期類型 / Whether it matches the expected type
59
+ */
24
60
  function typeNarrowed(actual, expression = true, message) {
25
- var _expression2;
26
-
27
- (_expression2 = expression) !== null && _expression2 !== void 0 ? _expression2 : expression = true;
28
-
29
- if (typeof expression === 'function') {
30
- expression = !!expression(actual);
31
- }
32
-
61
+ expression = _handleExpression(actual, expression);
33
62
  if (expression !== true) {
34
63
  expression = false;
35
64
  }
36
-
37
65
  return expression;
38
66
  }
39
67
 
40
- exports["default"] = typePredicates;
68
+ exports._handleExpression = _handleExpression;
69
+ exports.default = typePredicates;
41
70
  exports.typeNarrowed = typeNarrowed;
42
71
  exports.typePredicates = typePredicates;
43
72
  //# sourceMappingURL=index.cjs.development.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.development.cjs","sources":["../src/index.ts"],"sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * use asserts for make type predicates work\n *\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t})\n\t}\n}\n\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"],"names":["expected","expression"],"mappings":";;;;;;;;;;;EAAA,IAAwC,OAAA,UAAA,KAAA,UAAA,EAAA;;;;;;;;AAoBrCA,MAAAA,QAAAA,EAAAA,UAAAA;;AAID,GAAA;;;;;EASA,CAAA,YAAA,GAAA,UAAA,MAAA,IAAA,IAAA,YAAA,KAAA,KAAA,CAAA,GAAA,YAAA,GAAA,UAAA,GAAA,IAAA,CAAA;;aAGGC,eAAA,UAAmB,EAAA;iBAEZ,CAAA,CAAA,UAAA,CAAA,MAAA,CAAA,CAAA;;;gBAGX;AACA,IAAA,UAAA,GAAA,KAAA,CAAA;AAED,GAAA;;;AAxBG;;;;;;"}
1
+ {"version":3,"file":"index.cjs.development.cjs","sources":["../src/index.ts"],"sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * 處理錶達式,回傳布林值結果\n * Handle expression and return boolean result\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 錶達式,可為布林值或函式 / Expression, can be boolean or function\n * @returns 布林值結果 / Boolean result\n */\nexport function _handleExpression<T, P = any>(actual: T | P, expression: boolean | ((actual: T | P) => any) = true)\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\treturn expression\n}\n\n/**\n * 使用斷言(assert)讓類型斷言(type predicates)運作\n * Use asserts for make type predicates work\n *\n * 此函式結合了 TypeScript 的斷言函式與類型斷言功能,\n * 允許在運行時驗證類型並在編譯時縮小類型範圍\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 斷言條件,可為布林值或函式 / Assertion condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @param ignoreExpression - 是否忽略錶達式結果(可選)/ Whether to ignore expression result (optional)\n * @throws 當錶達式結果為 false 時拋出 AssertionError / Throws AssertionError when expression is false\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t\toperator: 'typePredicates',\n\t\t})\n\t}\n}\n\n/**\n * 類型收窄函式,回傳類型斷言結果\n * Type narrowing function, returns type predicate result\n *\n * 此函式不會拋出錯誤,而是回傳布林值表示是否符合預期類型\n * 可用於需要條件邏輯而非斷言的場景\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 驗證條件,可為布林值或函式 / Validation condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @returns 是否符合預期類型 / Whether it matches the expected type\n */\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"],"names":["expression","actual","expected","operator"],"mappings":";;;;;;;;AA8CG;;AA9CH;;;;;;;;AAUA,GAAA;SAEWA,UAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;MAkCRC,MAAA;AAEAC,MAAAA,QAAC,EAAAF,UAAA;MAEHG,QAAA,EAAA,gBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,2 +1,26 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("assert");function typePredicates(t,r=!0,o,a){var s;if(null!==(s=r)&&void 0!==s||(r=!0),"function"==typeof r&&(r=!!r(t)),!0!==r&&!0!==a)throw new e.AssertionError({message:null!=o?o:`actual ${t} not as expected`,actual:t,expected:r})}exports.default=typePredicates,exports.typeNarrowed=function typeNarrowed(e,t=!0,r){var o;return null!==(o=t)&&void 0!==o||(t=!0),"function"==typeof t&&(t=!!t(e)),!0!==t&&(t=!1),t},exports.typePredicates=typePredicates;
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: !0
5
+ });
6
+
7
+ var e = require("assert");
8
+
9
+ function _handleExpression(e, t = !0) {
10
+ return null != t || (t = !0), "function" == typeof t && (t = !!t(e)), t;
11
+ }
12
+
13
+ function typePredicates(t, r = !0, s, n) {
14
+ if (!0 !== (r = _handleExpression(t, r)) && !0 !== n) throw new e.AssertionError({
15
+ message: null != s ? s : `actual ${t} not as expected`,
16
+ actual: t,
17
+ expected: r,
18
+ operator: "typePredicates"
19
+ });
20
+ }
21
+
22
+ exports._handleExpression = _handleExpression, exports.default = typePredicates,
23
+ exports.typeNarrowed = function typeNarrowed(e, t = !0, r) {
24
+ return !0 !== (t = _handleExpression(e, t)) && (t = !1), t;
25
+ }, exports.typePredicates = typePredicates;
2
26
  //# sourceMappingURL=index.cjs.production.min.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs.production.min.cjs","sources":["../src/index.ts"],"sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * use asserts for make type predicates work\n *\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t})\n\t}\n}\n\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"],"names":["expression","expected","_expression2","actual"],"mappings":"0KAAwC,mBAAAA,kHAoBrCC,SAAAA,qGAaD,QAAAC,EAAAF,SAAA,IAAAE,IAAAF,GAAA,GAGG,mBAAAA,QAEOA,EAAAG,aAIXH,GAAA,IAtBE"}
1
+ {"version":3,"file":"index.cjs.production.min.cjs","sources":["../src/index.ts"],"sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * 處理錶達式,回傳布林值結果\n * Handle expression and return boolean result\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 錶達式,可為布林值或函式 / Expression, can be boolean or function\n * @returns 布林值結果 / Boolean result\n */\nexport function _handleExpression<T, P = any>(actual: T | P, expression: boolean | ((actual: T | P) => any) = true)\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\treturn expression\n}\n\n/**\n * 使用斷言(assert)讓類型斷言(type predicates)運作\n * Use asserts for make type predicates work\n *\n * 此函式結合了 TypeScript 的斷言函式與類型斷言功能,\n * 允許在運行時驗證類型並在編譯時縮小類型範圍\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 斷言條件,可為布林值或函式 / Assertion condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @param ignoreExpression - 是否忽略錶達式結果(可選)/ Whether to ignore expression result (optional)\n * @throws 當錶達式結果為 false 時拋出 AssertionError / Throws AssertionError when expression is false\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t\toperator: 'typePredicates',\n\t\t})\n\t}\n}\n\n/**\n * 類型收窄函式,回傳類型斷言結果\n * Type narrowing function, returns type predicate result\n *\n * 此函式不會拋出錯誤,而是回傳布林值表示是否符合預期類型\n * 可用於需要條件邏輯而非斷言的場景\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 驗證條件,可為布林值或函式 / Validation condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @returns 是否符合預期類型 / Whether it matches the expected type\n */\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"],"names":["expression","actual","expected","operator"],"mappings":";;;;;;;;;wEAYWA;;;;;;IAkCRC;IAEAC,UAACF;IAEHG,UAAA;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -1,11 +1,44 @@
1
1
  /**
2
- * use asserts for make type predicates work
2
+ * 處理錶達式,回傳布林值結果
3
+ * Handle expression and return boolean result
3
4
  *
5
+ * @param actual - 實際值 / Actual value
6
+ * @param expression - 錶達式,可為布林值或函式 / Expression, can be boolean or function
7
+ * @returns 布林值結果 / Boolean result
8
+ */
9
+ export declare function _handleExpression<T, P = any>(actual: T | P, expression?: boolean | ((actual: T | P) => any)): boolean;
10
+ /**
11
+ * 使用斷言(assert)讓類型斷言(type predicates)運作
12
+ * Use asserts for make type predicates work
13
+ *
14
+ * 此函式結合了 TypeScript 的斷言函式與類型斷言功能,
15
+ * 允許在運行時驗證類型並在編譯時縮小類型範圍
16
+ *
17
+ * @param actual - 實際值 / Actual value
18
+ * @param expression - 斷言條件,可為布林值或函式 / Assertion condition, can be boolean or function
19
+ * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)
20
+ * @param ignoreExpression - 是否忽略錶達式結果(可選)/ Whether to ignore expression result (optional)
21
+ * @throws 當錶達式結果為 false 時拋出 AssertionError / Throws AssertionError when expression is false
4
22
  * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates
5
23
  * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
6
24
  */
7
25
  export declare function typePredicates<T, P = any>(actual: T | P, expression?: boolean | ((actual: T | P) => any), message?: string, ignoreExpression?: boolean): asserts actual is T;
26
+ /**
27
+ * 類型收窄函式,回傳類型斷言結果
28
+ * Type narrowing function, returns type predicate result
29
+ *
30
+ * 此函式不會拋出錯誤,而是回傳布林值表示是否符合預期類型
31
+ * 可用於需要條件邏輯而非斷言的場景
32
+ *
33
+ * @param actual - 實際值 / Actual value
34
+ * @param expression - 驗證條件,可為布林值或函式 / Validation condition, can be boolean or function
35
+ * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)
36
+ * @returns 是否符合預期類型 / Whether it matches the expected type
37
+ */
8
38
  export declare function typeNarrowed<T, P = any>(actual: T | P, expression?: boolean | ((actual: T | P) => any), message?: string): actual is T;
9
- export default typePredicates;
39
+
40
+ export {
41
+ typePredicates as default,
42
+ };
10
43
 
11
44
  export {};
@@ -1,20 +1,21 @@
1
1
  import { AssertionError as e } from "assert";
2
2
 
3
- function typePredicates(t, a = !0, r, o) {
4
- var n;
5
- if (null !== (n = a) && void 0 !== n || (a = !0), "function" == typeof a && (a = !!a(t)),
6
- !0 !== a && !0 !== o) throw new e({
7
- message: null != r ? r : `actual ${t} not as expected`,
3
+ function _handleExpression(e, t = !0) {
4
+ return null != t || (t = !0), "function" == typeof t && (t = !!t(e)), t;
5
+ }
6
+
7
+ function typePredicates(t, r = !0, n, a) {
8
+ if (!0 !== (r = _handleExpression(t, r)) && !0 !== a) throw new e({
9
+ message: null != n ? n : `actual ${t} not as expected`,
8
10
  actual: t,
9
- expected: a
11
+ expected: r,
12
+ operator: "typePredicates"
10
13
  });
11
14
  }
12
15
 
13
- function typeNarrowed(e, t = !0, a) {
14
- var r;
15
- return null !== (r = t) && void 0 !== r || (t = !0), "function" == typeof t && (t = !!t(e)),
16
- !0 !== t && (t = !1), t;
16
+ function typeNarrowed(e, t = !0, r) {
17
+ return !0 !== (t = _handleExpression(e, t)) && (t = !1), t;
17
18
  }
18
19
 
19
- export { typePredicates as default, typeNarrowed, typePredicates };
20
+ export { _handleExpression, typePredicates as default, typeNarrowed, typePredicates };
20
21
  //# sourceMappingURL=index.esm.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.mjs","sources":["../src/index.ts"],"sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * use asserts for make type predicates work\n *\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t})\n\t}\n}\n\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"],"names":["expression","expected","_expression2","actual"],"mappings":";;;;oDAAwC,qBAAAA;;;;IAoBrCC,UAAAA;;;;;;SAaD,UAAAC,IAAAF,WAAA,MAAAE,MAAAF,KAAA,IAGG,qBAAAA,YAEOA,EAAAG;eAIXH,KAAA;AAtBE;;"}
1
+ {"version":3,"file":"index.esm.mjs","sources":["../src/index.ts"],"sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * 處理錶達式,回傳布林值結果\n * Handle expression and return boolean result\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 錶達式,可為布林值或函式 / Expression, can be boolean or function\n * @returns 布林值結果 / Boolean result\n */\nexport function _handleExpression<T, P = any>(actual: T | P, expression: boolean | ((actual: T | P) => any) = true)\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\treturn expression\n}\n\n/**\n * 使用斷言(assert)讓類型斷言(type predicates)運作\n * Use asserts for make type predicates work\n *\n * 此函式結合了 TypeScript 的斷言函式與類型斷言功能,\n * 允許在運行時驗證類型並在編譯時縮小類型範圍\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 斷言條件,可為布林值或函式 / Assertion condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @param ignoreExpression - 是否忽略錶達式結果(可選)/ Whether to ignore expression result (optional)\n * @throws 當錶達式結果為 false 時拋出 AssertionError / Throws AssertionError when expression is false\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t\toperator: 'typePredicates',\n\t\t})\n\t}\n}\n\n/**\n * 類型收窄函式,回傳類型斷言結果\n * Type narrowing function, returns type predicate result\n *\n * 此函式不會拋出錯誤,而是回傳布林值表示是否符合預期類型\n * 可用於需要條件邏輯而非斷言的場景\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 驗證條件,可為布林值或函式 / Validation condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @returns 是否符合預期類型 / Whether it matches the expected type\n */\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"],"names":["expression","actual","expected","operator"],"mappings":";;;wEAYWA;;;;;;IAkCRC;IAEAC,UAACF;IAEHG,UAAA;;;;;;;;"}
@@ -4,40 +4,69 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.TsTypePredicates = {}, global.assert));
5
5
  })(this, (function (exports, assert) { 'use strict';
6
6
 
7
- function typePredicates(actual, expression = true, message, ignoreExpression) {
8
- var _expression;
9
-
10
- (_expression = expression) !== null && _expression !== void 0 ? _expression : expression = true;
11
-
7
+ /**
8
+ * 處理錶達式,回傳布林值結果
9
+ * Handle expression and return boolean result
10
+ *
11
+ * @param actual - 實際值 / Actual value
12
+ * @param expression - 錶達式,可為布林值或函式 / Expression, can be boolean or function
13
+ * @returns 布林值結果 / Boolean result
14
+ */
15
+ function _handleExpression(actual, expression = true) {
16
+ expression !== null && expression !== void 0 ? expression : expression = true;
12
17
  if (typeof expression === 'function') {
13
18
  expression = !!expression(actual);
14
19
  }
15
-
20
+ return expression;
21
+ }
22
+ /**
23
+ * 使用斷言(assert)讓類型斷言(type predicates)運作
24
+ * Use asserts for make type predicates work
25
+ *
26
+ * 此函式結合了 TypeScript 的斷言函式與類型斷言功能,
27
+ * 允許在運行時驗證類型並在編譯時縮小類型範圍
28
+ *
29
+ * @param actual - 實際值 / Actual value
30
+ * @param expression - 斷言條件,可為布林值或函式 / Assertion condition, can be boolean or function
31
+ * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)
32
+ * @param ignoreExpression - 是否忽略錶達式結果(可選)/ Whether to ignore expression result (optional)
33
+ * @throws 當錶達式結果為 false 時拋出 AssertionError / Throws AssertionError when expression is false
34
+ * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates
35
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
36
+ */
37
+ function typePredicates(actual, expression = true, message, ignoreExpression) {
38
+ expression = _handleExpression(actual, expression);
16
39
  if (expression !== true && ignoreExpression !== true) {
17
40
  throw new assert.AssertionError({
18
41
  message: message !== null && message !== void 0 ? message : `actual ${actual} not as expected`,
19
42
  actual,
20
- expected: expression
43
+ expected: expression,
44
+ operator: 'typePredicates'
21
45
  });
22
46
  }
23
47
  }
48
+ /**
49
+ * 類型收窄函式,回傳類型斷言結果
50
+ * Type narrowing function, returns type predicate result
51
+ *
52
+ * 此函式不會拋出錯誤,而是回傳布林值表示是否符合預期類型
53
+ * 可用於需要條件邏輯而非斷言的場景
54
+ *
55
+ * @param actual - 實際值 / Actual value
56
+ * @param expression - 驗證條件,可為布林值或函式 / Validation condition, can be boolean or function
57
+ * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)
58
+ * @returns 是否符合預期類型 / Whether it matches the expected type
59
+ */
24
60
  function typeNarrowed(actual, expression = true, message) {
25
- var _expression2;
26
-
27
- (_expression2 = expression) !== null && _expression2 !== void 0 ? _expression2 : expression = true;
28
-
29
- if (typeof expression === 'function') {
30
- expression = !!expression(actual);
31
- }
32
-
61
+ expression = _handleExpression(actual, expression);
33
62
  if (expression !== true) {
34
63
  expression = false;
35
64
  }
36
-
37
65
  return expression;
38
66
  }
39
67
 
40
- exports["default"] = typePredicates;
68
+ exports._handleExpression = _handleExpression;
69
+ exports.default = typePredicates;
41
70
  exports.typeNarrowed = typeNarrowed;
42
71
  exports.typePredicates = typePredicates;
43
72
 
@@ -1 +1 @@
1
- {"version":3,"file":"index.umd.development.cjs","sources":["../src/index.ts"],"sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * use asserts for make type predicates work\n *\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t})\n\t}\n}\n\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"],"names":["expected","expression"],"mappings":";;;;;;;;;;;GAAA,IAAwC,OAAA,UAAA,KAAA,UAAA,EAAA;;;;;;;;CAoBrCA,MAAAA,QAAAA,EAAAA,UAAAA;;CAID,GAAA;;;;;GASA,CAAA,YAAA,GAAA,UAAA,MAAA,IAAA,IAAA,YAAA,KAAA,KAAA,CAAA,GAAA,YAAA,GAAA,UAAA,GAAA,IAAA,CAAA;;cAGGC,eAAA,UAAmB,EAAA;kBAEZ,CAAA,CAAA,UAAA,CAAA,MAAA,CAAA,CAAA;;;iBAGX;CACA,IAAA,UAAA,GAAA,KAAA,CAAA;CAED,GAAA;;;CAxBG;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.umd.development.cjs","sources":["../src/index.ts"],"sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * 處理錶達式,回傳布林值結果\n * Handle expression and return boolean result\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 錶達式,可為布林值或函式 / Expression, can be boolean or function\n * @returns 布林值結果 / Boolean result\n */\nexport function _handleExpression<T, P = any>(actual: T | P, expression: boolean | ((actual: T | P) => any) = true)\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\treturn expression\n}\n\n/**\n * 使用斷言(assert)讓類型斷言(type predicates)運作\n * Use asserts for make type predicates work\n *\n * 此函式結合了 TypeScript 的斷言函式與類型斷言功能,\n * 允許在運行時驗證類型並在編譯時縮小類型範圍\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 斷言條件,可為布林值或函式 / Assertion condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @param ignoreExpression - 是否忽略錶達式結果(可選)/ Whether to ignore expression result (optional)\n * @throws 當錶達式結果為 false 時拋出 AssertionError / Throws AssertionError when expression is false\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t\toperator: 'typePredicates',\n\t\t})\n\t}\n}\n\n/**\n * 類型收窄函式,回傳類型斷言結果\n * Type narrowing function, returns type predicate result\n *\n * 此函式不會拋出錯誤,而是回傳布林值表示是否符合預期類型\n * 可用於需要條件邏輯而非斷言的場景\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 驗證條件,可為布林值或函式 / Validation condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @returns 是否符合預期類型 / Whether it matches the expected type\n */\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"],"names":["expression","actual","expected","operator"],"mappings":";;;;;;;;CA8CG;;CA9CH;;;;;;;;CAUA,GAAA;UAEWA,UAAA,CAAA;;;;;;;;;;;;;;;;;;;;;;OAkCRC,MAAA;CAEAC,MAAAA,QAAC,EAAAF,UAAA;OAEHG,QAAA,EAAA,gBAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -1,2 +1,22 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("assert")):"function"==typeof define&&define.amd?define(["exports","assert"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TsTypePredicates={},e.assert)}(this,(function(e,t){"use strict";function typePredicates(e,o=!0,n,r){var i;if(null!==(i=o)&&void 0!==i||(o=!0),"function"==typeof o&&(o=!!o(e)),!0!==o&&!0!==r)throw new t.AssertionError({message:null!=n?n:`actual ${e} not as expected`,actual:e,expected:o})}e.default=typePredicates,e.typeNarrowed=function typeNarrowed(e,t=!0,o){var n;return null!==(n=t)&&void 0!==n||(t=!0),"function"==typeof t&&(t=!!t(e)),!0!==t&&(t=!1),t},e.typePredicates=typePredicates,Object.defineProperty(e,"__esModule",{value:!0})}));
1
+ !function(e, t) {
2
+ "object" == typeof exports && "undefined" != typeof module ? t(exports, require("assert")) : "function" == typeof define && define.amd ? define([ "exports", "assert" ], t) : t((e = "undefined" != typeof globalThis ? globalThis : e || self).TsTypePredicates = {}, e.assert);
3
+ }(this, function(e, t) {
4
+ "use strict";
5
+ function _handleExpression(e, t = !0) {
6
+ return null != t || (t = !0), "function" == typeof t && (t = !!t(e)), t;
7
+ }
8
+ function typePredicates(e, n = !0, s, r) {
9
+ if (!0 !== (n = _handleExpression(e, n)) && !0 !== r) throw new t.AssertionError({
10
+ message: null != s ? s : `actual ${e} not as expected`,
11
+ actual: e,
12
+ expected: n,
13
+ operator: "typePredicates"
14
+ });
15
+ }
16
+ e._handleExpression = _handleExpression, e.default = typePredicates, e.typeNarrowed = function typeNarrowed(e, t = !0, n) {
17
+ return !0 !== (t = _handleExpression(e, t)) && (t = !1), t;
18
+ }, e.typePredicates = typePredicates, Object.defineProperty(e, "__esModule", {
19
+ value: !0
20
+ });
21
+ });
2
22
  //# sourceMappingURL=index.umd.production.min.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.umd.production.min.cjs","sources":["../src/index.ts"],"sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * use asserts for make type predicates work\n *\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t})\n\t}\n}\n\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"],"names":["expression","expected","_expression2","actual"],"mappings":"4WAAwC,mBAAAA,kHAoBrCC,SAAAA,yFAaD,QAAAC,EAAAF,SAAA,IAAAE,IAAAF,GAAA,GAGG,mBAAAA,QAEOA,EAAAG,aAIXH,GAAA,IAtBE"}
1
+ {"version":3,"file":"index.umd.production.min.cjs","sources":["../src/index.ts"],"sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * 處理錶達式,回傳布林值結果\n * Handle expression and return boolean result\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 錶達式,可為布林值或函式 / Expression, can be boolean or function\n * @returns 布林值結果 / Boolean result\n */\nexport function _handleExpression<T, P = any>(actual: T | P, expression: boolean | ((actual: T | P) => any) = true)\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\treturn expression\n}\n\n/**\n * 使用斷言(assert)讓類型斷言(type predicates)運作\n * Use asserts for make type predicates work\n *\n * 此函式結合了 TypeScript 的斷言函式與類型斷言功能,\n * 允許在運行時驗證類型並在編譯時縮小類型範圍\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 斷言條件,可為布林值或函式 / Assertion condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @param ignoreExpression - 是否忽略錶達式結果(可選)/ Whether to ignore expression result (optional)\n * @throws 當錶達式結果為 false 時拋出 AssertionError / Throws AssertionError when expression is false\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t\toperator: 'typePredicates',\n\t\t})\n\t}\n}\n\n/**\n * 類型收窄函式,回傳類型斷言結果\n * Type narrowing function, returns type predicate result\n *\n * 此函式不會拋出錯誤,而是回傳布林值表示是否符合預期類型\n * 可用於需要條件邏輯而非斷言的場景\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 驗證條件,可為布林值或函式 / Validation condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @returns 是否符合預期類型 / Whether it matches the expected type\n */\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"],"names":["expression","actual","expected","operator"],"mappings":";;;;;0EAYWA;;;;;MAkCRC;MAEAC,UAACF;MAEHG,UAAA;;;;;;;;"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ts-type-predicates",
3
- "version": "1.0.8",
4
- "description": "use asserts for make type predicates work",
3
+ "version": "1.0.10",
4
+ "description": "使用斷言(assert)讓類型斷言(type predicates)運作的實用工具函式 | Use asserts to make type predicates work with better runtime validation",
5
5
  "keywords": [
6
6
  ".d.ts",
7
7
  "@types",
@@ -35,15 +35,16 @@
35
35
  },
36
36
  "repository": {
37
37
  "type": "git",
38
- "url": "git+https://github.com/bluelovers/ws-ts-type.git"
38
+ "url": "git+https://github.com/bluelovers/ws-ts-type.git",
39
+ "directory": "packages/ts-type-predicates"
39
40
  },
40
41
  "license": "ISC",
41
42
  "author": "bluelovers",
42
43
  "exports": {
43
44
  ".": {
44
45
  "types": "./dist/index.d.ts",
45
- "import": "./dist/index.esm.mjs",
46
- "require": "./dist/index.cjs"
46
+ "require": "./dist/index.cjs",
47
+ "import": "./dist/index.esm.mjs"
47
48
  },
48
49
  "./package.json": "./package.json"
49
50
  },
@@ -55,14 +56,19 @@
55
56
  "test": "test"
56
57
  },
57
58
  "scripts": {
59
+ "coverage": "yarn run test -- --coverage",
58
60
  "lint": "ynpx --quiet eslint -- **/*.ts",
59
- "test": "echo \"Error: no test specified\"",
61
+ "test": "jest --passWithNoTests",
60
62
  "test:jest": "ynpx --quiet jest -- --coverage",
63
+ "test:jest:coverage": "node --run test:jest -- --coverage",
64
+ "test:jest:snapshot": "yarn run test:jest -- -u",
61
65
  "test:mocha": "ynpx --quiet -p ts-node -p mocha mocha -- --require ts-node/register \"!(node_modules)/**/*.{test,spec}.{ts,tsx}\"",
66
+ "test:snapshot": "yarn run test -- -u",
67
+ "test:tsd": "ynpx tsd",
62
68
  "posttest": "yarn run build",
63
69
  "build": "yarn run build:tsdx && yarn run build:dts",
64
70
  "build:dts": "ynpx dts-bundle-generator -o ./dist/index.d.ts ./src/index.ts --no-banner & echo build:dts",
65
- "build:tsdx": "ynpx @bluelovers/tsdx build --target node --name index",
71
+ "build:tsdx": "tsdx build --target node --name index",
66
72
  "preversion": "echo preversion && yarn run test",
67
73
  "version": "echo version",
68
74
  "postversion": "echo postversion",
@@ -78,10 +84,5 @@
78
84
  "sort-package-json": "ynpx --quiet yarn-tool -- sort",
79
85
  "tsc:showConfig": "ynpx get-current-tsconfig -p"
80
86
  },
81
- "devDependencies": {
82
- "@bluelovers/tsconfig": "*",
83
- "@types/node": "*"
84
- },
85
- "packageManager": "yarn@^1.22.11",
86
- "gitHead": "4eedf3cb6b9ee750e22993560eb6aef919873112"
87
+ "gitHead": "57f82cba146cfef0d0f3f138e5ec736cd17f040d"
87
88
  }
package/src/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA,mCAAwC;AAExC;;;;;GAKG;AACH,SAAgB,cAAc,CAAa,MAAa,EAAE,aAAkD,IAAI,EAAE,OAAgB,EAAE,gBAA0B;IAE7J,UAAU,aAAV,UAAU,cAAV,UAAU,IAAV,UAAU,GAAK,IAAI,EAAC;IAEpB,IAAI,OAAO,UAAU,KAAK,UAAU,EACpC;QACC,UAAU,GAAG,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;KAClC;IAED,IAAI,UAAU,KAAK,IAAI,IAAI,gBAAgB,KAAK,IAAI,EACpD;QACC,MAAM,IAAI,uBAAc,CAAC;YACxB,OAAO,EAAE,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,UAAU,MAAM,kBAAkB;YACtD,MAAM;YACN,QAAQ,EAAE,UAAU;SACpB,CAAC,CAAA;KACF;AACF,CAAC;AAjBD,wCAiBC;AAED,SAAgB,YAAY,CAAa,MAAa,EAAE,aAAkD,IAAI,EAAE,OAAgB;IAE/H,UAAU,aAAV,UAAU,cAAV,UAAU,IAAV,UAAU,GAAK,IAAI,EAAC;IAEpB,IAAI,OAAO,UAAU,KAAK,UAAU,EACpC;QACC,UAAU,GAAG,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;KAClC;IAED,IAAI,UAAU,KAAK,IAAI,EACvB;QACC,UAAU,GAAG,KAAK,CAAC;KACnB;IAED,OAAO,UAAU,CAAA;AAClB,CAAC;AAfD,oCAeC;AAED,kBAAe,cAAc,CAAC","sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * use asserts for make type predicates work\n *\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t})\n\t}\n}\n\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;AAUA,8CAUC;AAiBD,wCAaC;AAcD,oCAUC;AA1ED,mCAAwC;AAExC;;;;;;;GAOG;AACH,SAAgB,iBAAiB,CAAa,MAAa,EAAE,aAAiD,IAAI;IAEjH,UAAU,aAAV,UAAU,cAAV,UAAU,IAAV,UAAU,GAAK,IAAI,EAAC;IAEpB,IAAI,OAAO,UAAU,KAAK,UAAU,EACpC,CAAC;QACA,UAAU,GAAG,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;IACnC,CAAC;IAED,OAAO,UAAU,CAAA;AAClB,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,cAAc,CAAa,MAAa,EAAE,aAAkD,IAAI,EAAE,OAAgB,EAAE,gBAA0B;IAE7J,UAAU,GAAG,iBAAiB,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAEnD,IAAI,UAAU,KAAK,IAAI,IAAI,gBAAgB,KAAK,IAAI,EACpD,CAAC;QACA,MAAM,IAAI,uBAAc,CAAC;YACxB,OAAO,EAAE,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,UAAU,MAAM,kBAAkB;YACtD,MAAM;YACN,QAAQ,EAAE,UAAU;YACpB,QAAQ,EAAE,gBAAgB;SAC1B,CAAC,CAAA;IACH,CAAC;AACF,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,YAAY,CAAa,MAAa,EAAE,aAAkD,IAAI,EAAE,OAAgB;IAE/H,UAAU,GAAG,iBAAiB,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;IAEnD,IAAI,UAAU,KAAK,IAAI,EACvB,CAAC;QACA,UAAU,GAAG,KAAK,CAAC;IACpB,CAAC;IAED,OAAO,UAAU,CAAA;AAClB,CAAC;AAED,kBAAe,cAAc,CAAC","sourcesContent":["import { AssertionError } from 'assert';\n\n/**\n * 處理錶達式,回傳布林值結果\n * Handle expression and return boolean result\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 錶達式,可為布林值或函式 / Expression, can be boolean or function\n * @returns 布林值結果 / Boolean result\n */\nexport function _handleExpression<T, P = any>(actual: T | P, expression: boolean | ((actual: T | P) => any) = true)\n{\n\texpression ??= true;\n\n\tif (typeof expression === 'function')\n\t{\n\t\texpression = !!expression(actual);\n\t}\n\n\treturn expression\n}\n\n/**\n * 使用斷言(assert)讓類型斷言(type predicates)運作\n * Use asserts for make type predicates work\n *\n * 此函式結合了 TypeScript 的斷言函式與類型斷言功能,\n * 允許在運行時驗證類型並在編譯時縮小類型範圍\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 斷言條件,可為布林值或函式 / Assertion condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @param ignoreExpression - 是否忽略錶達式結果(可選)/ Whether to ignore expression result (optional)\n * @throws 當錶達式結果為 false 時拋出 AssertionError / Throws AssertionError when expression is false\n * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates\n * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions\n */\nexport function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true && ignoreExpression !== true)\n\t{\n\t\tthrow new AssertionError({\n\t\t\tmessage: message ?? `actual ${actual} not as expected`,\n\t\t\tactual,\n\t\t\texpected: expression,\n\t\t\toperator: 'typePredicates',\n\t\t})\n\t}\n}\n\n/**\n * 類型收窄函式,回傳類型斷言結果\n * Type narrowing function, returns type predicate result\n *\n * 此函式不會拋出錯誤,而是回傳布林值表示是否符合預期類型\n * 可用於需要條件邏輯而非斷言的場景\n *\n * @param actual - 實際值 / Actual value\n * @param expression - 驗證條件,可為布林值或函式 / Validation condition, can be boolean or function\n * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)\n * @returns 是否符合預期類型 / Whether it matches the expected type\n */\nexport function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T\n{\n\texpression = _handleExpression(actual, expression);\n\n\tif (expression !== true)\n\t{\n\t\texpression = false;\n\t}\n\n\treturn expression\n}\n\nexport default typePredicates;\n"]}
package/src/index.ts ADDED
@@ -0,0 +1,77 @@
1
+ import { AssertionError } from 'assert';
2
+
3
+ /**
4
+ * 處理錶達式,回傳布林值結果
5
+ * Handle expression and return boolean result
6
+ *
7
+ * @param actual - 實際值 / Actual value
8
+ * @param expression - 錶達式,可為布林值或函式 / Expression, can be boolean or function
9
+ * @returns 布林值結果 / Boolean result
10
+ */
11
+ export function _handleExpression<T, P = any>(actual: T | P, expression: boolean | ((actual: T | P) => any) = true)
12
+ {
13
+ expression ??= true;
14
+
15
+ if (typeof expression === 'function')
16
+ {
17
+ expression = !!expression(actual);
18
+ }
19
+
20
+ return expression
21
+ }
22
+
23
+ /**
24
+ * 使用斷言(assert)讓類型斷言(type predicates)運作
25
+ * Use asserts for make type predicates work
26
+ *
27
+ * 此函式結合了 TypeScript 的斷言函式與類型斷言功能,
28
+ * 允許在運行時驗證類型並在編譯時縮小類型範圍
29
+ *
30
+ * @param actual - 實際值 / Actual value
31
+ * @param expression - 斷言條件,可為布林值或函式 / Assertion condition, can be boolean or function
32
+ * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)
33
+ * @param ignoreExpression - 是否忽略錶達式結果(可選)/ Whether to ignore expression result (optional)
34
+ * @throws 當錶達式結果為 false 時拋出 AssertionError / Throws AssertionError when expression is false
35
+ * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates
36
+ * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
37
+ */
38
+ export function typePredicates<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string, ignoreExpression?: boolean): asserts actual is T
39
+ {
40
+ expression = _handleExpression(actual, expression);
41
+
42
+ if (expression !== true && ignoreExpression !== true)
43
+ {
44
+ throw new AssertionError({
45
+ message: message ?? `actual ${actual} not as expected`,
46
+ actual,
47
+ expected: expression,
48
+ operator: 'typePredicates',
49
+ })
50
+ }
51
+ }
52
+
53
+ /**
54
+ * 類型收窄函式,回傳類型斷言結果
55
+ * Type narrowing function, returns type predicate result
56
+ *
57
+ * 此函式不會拋出錯誤,而是回傳布林值表示是否符合預期類型
58
+ * 可用於需要條件邏輯而非斷言的場景
59
+ *
60
+ * @param actual - 實際值 / Actual value
61
+ * @param expression - 驗證條件,可為布林值或函式 / Validation condition, can be boolean or function
62
+ * @param message - 自訂錯誤訊息(可選)/ Custom error message (optional)
63
+ * @returns 是否符合預期類型 / Whether it matches the expected type
64
+ */
65
+ export function typeNarrowed<T, P = any>(actual: T | P, expression : boolean | ((actual: T | P) => any) = true, message?: string): actual is T
66
+ {
67
+ expression = _handleExpression(actual, expression);
68
+
69
+ if (expression !== true)
70
+ {
71
+ expression = false;
72
+ }
73
+
74
+ return expression
75
+ }
76
+
77
+ export default typePredicates;
package/src/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- /**
2
- * use asserts for make type predicates work
3
- *
4
- * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates
5
- * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
6
- */
7
- export declare function typePredicates<T, P = any>(actual: T | P, expression?: boolean | ((actual: T | P) => any), message?: string, ignoreExpression?: boolean): asserts actual is T;
8
- export declare function typeNarrowed<T, P = any>(actual: T | P, expression?: boolean | ((actual: T | P) => any), message?: string): actual is T;
9
- export default typePredicates;
package/src/index.js DELETED
@@ -1,37 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.typeNarrowed = exports.typePredicates = void 0;
4
- const assert_1 = require("assert");
5
- /**
6
- * use asserts for make type predicates work
7
- *
8
- * @see https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates
9
- * @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions
10
- */
11
- function typePredicates(actual, expression = true, message, ignoreExpression) {
12
- expression !== null && expression !== void 0 ? expression : (expression = true);
13
- if (typeof expression === 'function') {
14
- expression = !!expression(actual);
15
- }
16
- if (expression !== true && ignoreExpression !== true) {
17
- throw new assert_1.AssertionError({
18
- message: message !== null && message !== void 0 ? message : `actual ${actual} not as expected`,
19
- actual,
20
- expected: expression,
21
- });
22
- }
23
- }
24
- exports.typePredicates = typePredicates;
25
- function typeNarrowed(actual, expression = true, message) {
26
- expression !== null && expression !== void 0 ? expression : (expression = true);
27
- if (typeof expression === 'function') {
28
- expression = !!expression(actual);
29
- }
30
- if (expression !== true) {
31
- expression = false;
32
- }
33
- return expression;
34
- }
35
- exports.typeNarrowed = typeNarrowed;
36
- exports.default = typePredicates;
37
- //# sourceMappingURL=index.js.map