Skip to content

Commit

Permalink
2. [release:1.1.0]
Browse files Browse the repository at this point in the history
  • Loading branch information
just-do-halee committed Oct 5, 2021
1 parent e123778 commit e520746
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 30 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
/node_modules
/coverage
/coverage
/dist
18 changes: 18 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

---

Expand All @@ -31,6 +32,8 @@ yarn add rusultts
## **Examples**<br>

```ts
import { Result, Ok, Err } from 'rusultts';

function tryParse(token: string): Result<SomeType> {
// ... heavy stuffs
if (somethingWrong) {
Expand Down Expand Up @@ -58,6 +61,8 @@ type Result<T> = ResultBox<T, null>;
```

```ts
import { ResultBox, Ok, Err } from 'rusultts';

function divide(a: number, b: number): ResultBox<number, number> {
if (b === 0) {
return Err.new(`b cannot be 0.`, b);
Expand Down
5 changes: 4 additions & 1 deletion jest.config.js → jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default {
import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
coverageDirectory: 'coverage',
coverageReporters: ['json-summary', 'text', 'lcov'],
roots: ['<rootDir>'],
Expand All @@ -7,3 +9,4 @@ export default {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
};
export default config;
93 changes: 92 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <just.do.halee@gmail.com>",
"license": "MIT",
"scripts": {
"build": "tsc",
"test": "jest",
"cover": "jest --ci --coverage --maxWorkers=2 && jest-coverage-badges"
},
Expand Down Expand Up @@ -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"
]
}
9 changes: 6 additions & 3 deletions src/rusultts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// rusultTs

type ResultObject<T> = {
export type ResultObject<T> = {
readonly error?: Error;
readonly value: T;
};
Expand Down Expand Up @@ -109,10 +109,13 @@ export class Err<T, E> extends ResultBox<T, E> {
}
/**
*
* @param e Error
* @param e Error | unknown
* @returns [`error.message`, `<E>.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] || '', ''];
Expand Down
4 changes: 1 addition & 3 deletions test/rusultts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down
34 changes: 17 additions & 17 deletions test/test.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,53 @@ 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 {
return new Stack();
}
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<IStack> {
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<string> {
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 + ' ';
Expand Down
22 changes: 22 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}

0 comments on commit e520746

Please sign in to comment.