From e5207465250ed114f7adb49b6d94e53cede98f92 Mon Sep 17 00:00:00 2001 From: just-do-halee Date: Tue, 5 Oct 2021 17:59:20 +0900 Subject: [PATCH] 2. [release:1.1.0] --- .gitignore | 4 +- .npmignore | 18 +++++++ CHANGELOG.md | 6 +++ README.md | 5 ++ jest.config.js => jest.config.ts | 5 +- package-lock.json | 93 +++++++++++++++++++++++++++++++- package.json | 10 ++-- src/rusultts.ts | 9 ++-- test/rusultts.test.ts | 4 +- test/test.types.ts | 34 ++++++------ tsconfig.json | 22 ++++++++ 11 files changed, 180 insertions(+), 30 deletions(-) create mode 100644 .npmignore create mode 100644 CHANGELOG.md rename jest.config.js => jest.config.ts (66%) create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore index b1bc6fe..f84c5ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ +.DS_Store /node_modules -/coverage \ No newline at end of file +/coverage +/dist \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..315e1d4 --- /dev/null +++ b/.npmignore @@ -0,0 +1,18 @@ +.*.swp +._* +.DS_Store +.git +.hg +.npmrc +.lock-wscript +.svn +.wafpickle-* +config.gypi +CVS +npm-debug.log +.vscode +.github + +/dist +/test +/badges \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..0cee536 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,6 @@ +## 1.1.0 (October 5, 2021) + +### Release 1.1.0 + +- tsc compiled (typescript 4.4.3) +- Err.eSplit now can take unknown value type diff --git a/README.md b/README.md index 58f7853..d6e8568 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Rust **_Result Implementation for Typescript_**, simply. i.e. Modern error handl [![NPM Downloads][downloads-image]][downloads-url] [![CI](https://github.com/just-do-halee/rusultts/actions/workflows/main.yml/badge.svg)](https://github.com/just-do-halee/rusultts/actions/workflows/main.yml) [![License][license-image]][license-url] +[[changelog]](CHANGELOG.md) --- @@ -31,6 +32,8 @@ yarn add rusultts ## **Examples**
```ts +import { Result, Ok, Err } from 'rusultts'; + function tryParse(token: string): Result { // ... heavy stuffs if (somethingWrong) { @@ -58,6 +61,8 @@ type Result = ResultBox; ``` ```ts +import { ResultBox, Ok, Err } from 'rusultts'; + function divide(a: number, b: number): ResultBox { if (b === 0) { return Err.new(`b cannot be 0.`, b); diff --git a/jest.config.js b/jest.config.ts similarity index 66% rename from jest.config.js rename to jest.config.ts index 0f255e9..82a9d95 100644 --- a/jest.config.js +++ b/jest.config.ts @@ -1,4 +1,6 @@ -export default { +import type { Config } from '@jest/types'; + +const config: Config.InitialOptions = { coverageDirectory: 'coverage', coverageReporters: ['json-summary', 'text', 'lcov'], roots: [''], @@ -7,3 +9,4 @@ export default { '^.+\\.(ts|tsx)$': 'ts-jest', }, }; +export default config; diff --git a/package-lock.json b/package-lock.json index 79aa12e..06d4923 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "rusultts", - "version": "1.0.0", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -442,6 +442,21 @@ "integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==", "dev": true }, + "@cspotcode/source-map-consumer": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz", + "integrity": "sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==", + "dev": true + }, + "@cspotcode/source-map-support": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.6.1.tgz", + "integrity": "sha512-DX3Z+T5dt1ockmPdobJS/FAsQPW4V4SrWEhD2iYQT2Cb2tQsiMnYxrcUH9By/Z3B+v0S5LMBkQtV/XOBbpLEOg==", + "dev": true, + "requires": { + "@cspotcode/source-map-consumer": "0.8.0" + } + }, "@istanbuljs/load-nyc-config": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz", @@ -675,6 +690,30 @@ "integrity": "sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==", "dev": true }, + "@tsconfig/node10": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.8.tgz", + "integrity": "sha512-6XFfSQmMgq0CFLY1MslA/CPUfhIL919M1rMsa5lP2P097N2Wd1sSX0tx1u4olM16fLNhtHZpRhedZJphNJqmZg==", + "dev": true + }, + "@tsconfig/node12": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.9.tgz", + "integrity": "sha512-/yBMcem+fbvhSREH+s14YJi18sp7J9jpuhYByADT2rypfajMZZN4WQ6zBGgBKp53NKmqI36wFYDb3yaMPurITw==", + "dev": true + }, + "@tsconfig/node14": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.1.tgz", + "integrity": "sha512-509r2+yARFfHHE7T6Puu2jjkoycftovhXRqW328PDXTVGKihlb1P8Z9mMZH04ebyajfRY7dedfGynlrFHJUQCg==", + "dev": true + }, + "@tsconfig/node16": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.2.tgz", + "integrity": "sha512-eZxlbI8GZscaGS7kkc/trHTT5xgrjH3/1n2JDwusC9iahPKWMRvRjJSAN5mCXviuTGQ/lHnhvv8Q1YTpnfz9gA==", + "dev": true + }, "@types/babel__core": { "version": "7.1.16", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.16.tgz", @@ -871,6 +910,12 @@ "picomatch": "^2.0.4" } }, + "arg": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==", + "dev": true + }, "argparse": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", @@ -1133,6 +1178,12 @@ "safe-buffer": "~5.1.1" } }, + "create-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", + "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==", + "dev": true + }, "cross-spawn": { "version": "7.0.3", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", @@ -1223,6 +1274,12 @@ "integrity": "sha512-TLz+x/vEXm/Y7P7wn1EJFNLxYpUD4TgMosxY6fAVJUnJMbupHBOncxyWUG9OpTaH9EBD7uFI5LfEgmMOc54DsA==", "dev": true }, + "diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "dev": true + }, "diff-sequences": { "version": "27.0.6", "resolved": "https://registry.npmjs.org/diff-sequences/-/diff-sequences-27.0.6.tgz", @@ -2913,6 +2970,34 @@ } } }, + "ts-node": { + "version": "10.2.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.2.1.tgz", + "integrity": "sha512-hCnyOyuGmD5wHleOQX6NIjJtYVIO8bPP8F2acWkB4W06wdlkgyvJtubO/I9NkI88hCFECbsEgoLc0VNkYmcSfw==", + "dev": true, + "requires": { + "@cspotcode/source-map-support": "0.6.1", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "yn": "3.1.1" + }, + "dependencies": { + "acorn-walk": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz", + "integrity": "sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==", + "dev": true + } + } + }, "type-check": { "version": "0.3.2", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.3.2.tgz", @@ -3127,6 +3212,12 @@ "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz", "integrity": "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==", "dev": true + }, + "yn": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", + "dev": true } } } diff --git a/package.json b/package.json index 814ffac..686b68b 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,13 @@ { "name": "rusultts", - "main": "src/rusultts.ts", - "type": "module", - "version": "1.0.1", + "main": "./dist/rusultts.ts", + "types": "./dist/rusultts.d.ts", + "version": "1.1.0", "description": "Rust Result Implementation for Typescript, simply. i.e. Modern error handling library.", "author": "just-do-halee ", "license": "MIT", "scripts": { + "build": "tsc", "test": "jest", "cover": "jest --ci --coverage --maxWorkers=2 && jest-coverage-badges" }, @@ -35,9 +36,10 @@ "jest": "^27.2.4", "jest-coverage-badges": "^1.1.2", "ts-jest": "^27.0.5", + "ts-node": "^10.2.1", "typescript": "^4.4.3" }, "files": [ - "src/" + "/dist" ] } diff --git a/src/rusultts.ts b/src/rusultts.ts index 31ec0a8..d3b304f 100644 --- a/src/rusultts.ts +++ b/src/rusultts.ts @@ -1,6 +1,6 @@ // rusultTs -type ResultObject = { +export type ResultObject = { readonly error?: Error; readonly value: T; }; @@ -109,10 +109,13 @@ export class Err extends ResultBox { } /** * - * @param e Error + * @param e Error | unknown * @returns [`error.message`, `.toString()`] or ***[error.message, ''] (not found)*** */ - static eSplit(e: Error): [string, string] { + static eSplit(e: Error | unknown): [string, string] { + if (!(e instanceof Error)) { + return ['', '']; + } let val: string[] = e.message.split(':--> ', 2); if (val.length !== 2) { val = [val[0] || '', '']; diff --git a/test/rusultts.test.ts b/test/rusultts.test.ts index ce9b885..188445b 100644 --- a/test/rusultts.test.ts +++ b/test/rusultts.test.ts @@ -22,11 +22,8 @@ describe('make some results', () => { return Ok.new(Result0.unwrap()); } catch (e) { - const splited = Err.eSplit(e); expect(errValue).toBeDefined(); - expect(splited[0]).toEqual('some error message'); if (errValue) { - expect(splited[1]).toEqual(String(errValue)); return Ok.new(errValue); } else { return Err.new(`testing error.`, null); @@ -83,6 +80,7 @@ describe('make some results', () => { test.unwrap(); throw new Error(`testing error.`); } catch (e) { + expect(Err.eSplit({ this: 'is unknown' })).toEqual(['', '']); expect(Err.eSplit(new Error(``))).toEqual(['', '']); expect(Err.eSplit(new Error(`fake Error`))[1]).toEqual(''); expect(Err.eSplit(e)[1]).toEqual(String(0)); diff --git a/test/test.types.ts b/test/test.types.ts index 0448a8f..448c711 100644 --- a/test/test.types.ts +++ b/test/test.types.ts @@ -14,21 +14,21 @@ export interface IStack { } export class Stack implements IStack { - #cap: number = 8; - #len: number = 0; - #lastNode: StrNode = { + private cap: number = 8; + private len: number = 0; + private lastNode: StrNode = { str: '', }; get length(): number { - return this.#len; + return this.len; } set capacity(value: number) { - if (value > this.#cap) { - this.#cap = value; + if (value > this.cap) { + this.cap = value; } } get capacity(): number { - return this.#cap; + return this.cap; } protected constructor() {} static new(): IStack { @@ -36,31 +36,31 @@ export class Stack implements IStack { } static withCapacity(cap: number): IStack { const vec = new Stack(); - vec.#cap = cap < 0 ? 0 : cap; + vec.cap = cap < 0 ? 0 : cap; return vec; } push(value: string): Result { - if (this.#len >= this.#cap) { + if (this.len >= this.cap) { return Err.new(`full of stack`, null); } - this.#lastNode = { - prev: this.#lastNode, + this.lastNode = { + prev: this.lastNode, str: value, }; - this.#len++; + this.len++; return Ok.new(this); } pop(): Result { - if (!this.#lastNode.prev) { + if (!this.lastNode.prev) { return Err.new(`out of bounds`, null); } - const str = this.#lastNode.str; - this.#lastNode = this.#lastNode.prev; - this.#len--; + const str = this.lastNode.str; + this.lastNode = this.lastNode.prev; + this.len--; return Ok.new(str); } print(): IStack { - let lastNode = this.#lastNode; + let lastNode = this.lastNode; let output = ''; while (lastNode.prev) { output += lastNode.str + ' '; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..f569cf1 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + /* Basic Options */ + "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */, + "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, + "lib": [ + "es2017", + "es7", + "es6", + "dom" + ] /* Specify library files to be included in the compilation. */, + "declaration": true /* Generates corresponding '.d.ts' file. */, + "declarationDir": "dist", + "outDir": "dist" /* Redirect output structure to the directory. */, + "strict": true /* Enable all strict type-checking options. */, + "noImplicitAny": true, + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, + "moduleResolution": "Node" + }, + "include": ["src/**/*"], + "exclude": ["node_modules", "dist", "test"] +}