1.0.2 • Published 3 years ago

@travic/try v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Try

ci npm version npm

Coverage lines Coverage functions Coverage branches Coverage statements

npm i @travic/try
or
yarn add @travic/try
import { Try } from '@travic/try';
// or
const { Try } = require('@travic/try');

// example use

test('Try should be success', () => {
	const res = Try.of(() => true),
		res2 = Try.of(() => false),
		res3 = Try.of(() => {});

	expect(res.isSuccess).toEqual(true);
	expect(res.get()).toEqual(true);
	expect(res2.isSuccess).toEqual(true);
	expect(res2.get()).toEqual(false);
	expect(res3.isSuccess).toEqual(true);
	expect(res3.get()).toEqual(undefined);
});

test('Try should have getOrElse result', () => {
	const res = Try.of(() => {
		throw new Error();
	}).getOrElse('Hello Else');

	expect(res).toEqual('Hello Else');
});

test('Try should perform onFailure method', () => {
	const res = Try.of(() => {
		throw new Error();
	})
		.onFailure(() => 'On Fail')
		.get();

	expect(res).toEqual('On Fail');
});

test('Try should give unhandled error in get', () => {
	const res = Try.of(() => {
		throw new Error();
	}).get();

	expect(res).toHaveProperty('unhandled', true);
});
1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago