Skip to content

Commit

Permalink
Updated dependencies to latest.
Browse files Browse the repository at this point in the history
Added basic example to readme.
Adding missing test case for constructor init data.
  • Loading branch information
mbrich committed Feb 8, 2024
1 parent a754112 commit b9ba87a
Show file tree
Hide file tree
Showing 9 changed files with 1,370 additions and 1,445 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ coverage
yarn-debug.log
yarn-error.log
test-report.xml
.git
.github
examples
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.6.9] - 2024-02-07
* Fixed `Fate` constructor bug where `fate.data` was not set to provided `init.data` when the value evaluated to falsy.


## [0.6.8] - 2023-06-16
* Fixed serialization bug where `errorCode` and `success` were not included during serialization, but were included when deserializing.

Expand Down
66 changes: 60 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@
![license](https://img.shields.io/github/license/toreda/fate?style=for-the-badge)

 

# `@toreda/fate`

Simple interface return a result and details or context about the result.
Return function results, errors, status, and logs with one object.


 

# Install

***With yarn (preferred):***
**_With yarn (preferred):_**

```bash
yarn add @toreda/fate
```

With NPM:

```bash
npm install @toreda/fate
```
Expand All @@ -33,6 +37,7 @@ npm install @toreda/fate
## Library Usage

### Typescript

```typescript
import {Fate} from '@toreda/fate';

Expand Down Expand Up @@ -68,26 +73,70 @@ const serialized = fate.serialize();

// Uses serialized state data to rebuild a fate
const fromSerial = new Fate({serialized});
```

## TypeScript

```typescript
import {Fate} from '@toreda/fate';

const fate = new Fate<never>();

if (fate.success()) {
console.info(`Success`);
} else {
console.info(`Failed`);
}
```

## Node
```typescript
const Fate = require('@toreda/fate');
import {Fate} from '@toreda/fate';

function isPositive(value: number): Fate<boolean> {
// Fate instance containing a boolean with an initial value `false`.
const fate = new Fate<boolean>({
data: false
});

// Sets success to false & sets an error code.
if (typeof value !== 'number') {
return fate.setErrorCode('non_number_value');
}


fate.data = value >= 0;

return fate.setSuccess(true);
}

const result = isPositive(1);
if (result.success()) {
// Function succeeded.
if (fate.data === true) {
console.info(`${value} is positive`);
} else {
console.info(`${value} is negative`);
}
} else {
console.error(`isPositive failed with error code: ${result.errorCode()});
}
```

&nbsp;

# Build

Build (or rebuild) the config package:

***With Yarn (preferred):***
**_With Yarn (preferred):_**

```bash
yarn install
yarn build
```

With NPM:

```bash
npm install
npm run-script build
Expand All @@ -99,27 +148,32 @@ npm run-script build

Config implements unit tests using jest. Run the following commands from the directory where config has been installed.

***With yarn (preferred):***
**_With yarn (preferred):_**

```
yarn install
yarn test
```

With NPM:

```
npm install
npm run-script test
```

&nbsp;

# Legal

## License

[MIT](LICENSE) &copy; Toreda, Inc.

&nbsp;

## Copyright

Copyright &copy; 2019 - 2022 Toreda, Inc. All Rights Reserved.

https://www.toreda.com
3 changes: 2 additions & 1 deletion fate.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"LICENSE": true,
"node_modules": true,
"sonar-project.properties": true,
"yarn.lock": true
"yarn.lock": true,
"test-report.xml": true
}
}
}
33 changes: 18 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,34 @@
},
"prettier": "@toreda/prettier-config",
"homepage": "https://github.com/toreda/fate#readme",
"resolutions": {
"chokidar": "^3.0.0"
},
"devDependencies": {
"@swc/core": "^1.3.64",
"@swc/jest": "^0.2.26",
"@swc/core": "^1.4.0",
"@swc/jest": "^0.2.36",
"@toreda/build-tools": "^0.8.0",
"@toreda/eslint-config": "^2.2.0",
"@toreda/log": "^0.6.16",
"@toreda/prettier-config": "^1.0.1",
"@toreda/strong-types": "^0.28.5",
"@toreda/types": "^2.16.0",
"@types/gulp": "^4.0.9",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"@typescript-eslint/typescript-estree": "^5.59.11",
"eslint": "^8.42.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"@types/gulp": "^4.0.17",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.16",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"@typescript-eslint/typescript-estree": "^6.21.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"gulp": "^4.0.2",
"gulp-typescript": "^6.0.0-alpha.1",
"jest": "^29.5.0",
"jest": "^29.7.0",
"jest-sonar-reporter": "^2.0.0",
"prettier": "^2.8.8",
"ts-node": "^10.9.1",
"typescript": "^5.1.3"
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"dependencies": {}
}
3 changes: 1 addition & 2 deletions src/fate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class Fate<T = unknown> {

constructor(options: FateOptions<T> = {}) {
this.data = null;

this.errorLog = [];
this.messageLog = [];
this.errorThreshold = 0;
Expand Down Expand Up @@ -52,7 +51,7 @@ export class Fate<T = unknown> {
this.success(state.success);
}

if (options.data) {
if (typeof options.data !== 'undefined') {
this.data = options.data;
}

Expand Down
48 changes: 47 additions & 1 deletion tests/fate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
import {Fate} from '../src/fate';
import {typeMatch} from '@toreda/strong-types';

const INIT_VALUES: {
value: unknown;
label: string;
}[] = [
{
value: true,
label: 'boolean (true)'
},
{
value: false,
label: 'boolean (false)'
},
{
value: 1,
label: 'a truthy number (1)'
},
{
value: 0,
label: 'zero'
},
{
value: [],
label: 'an empty array'
},
{
value: [1, 2, 3, 4],
label: 'an array of numbers'
},
{
value: {},
label: 'an empty object'
}
];

describe('Fate', () => {
const instance = new Fate();
const options = {
Expand All @@ -20,7 +54,7 @@ describe('Fate', () => {
instance.data = null;
});

describe('Construction', () => {
describe('Constructor', () => {
it('should not throw with no args', () => {
expect(() => {
new Fate();
Expand Down Expand Up @@ -107,6 +141,18 @@ describe('Fate', () => {
console.log(result);
}).toThrow();
});

describe('Init Values', () => {
for (const init of INIT_VALUES) {
it(`should set data to init.data the value is ${init.label}`, () => {
const custom = new Fate<boolean>({
data: init.value as any
});

expect(custom.data).toBe(init.value);
});
}
});
});

describe(`Serialization`, () => {
Expand Down
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"traceResolution": false
},
"ts-node": {
"swc": true
"swc": true,
"compilerOptions": {
"noEmit": true
}
}
}
Loading

0 comments on commit b9ba87a

Please sign in to comment.