Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

[![NPM version](https://img.shields.io/npm/v/@zakodium/utils.svg)](https://www.npmjs.com/package/@zakodium/utils)
[![npm download](https://img.shields.io/npm/dm/@zakodium/utils.svg)](https://www.npmjs.com/package/@zakodium/utils)
[![test coverage](https://img.shields.io/codecov/c/github/@zakodium/utils.svg)](https://codecov.io/gh/@zakodium/utils)
[![license](https://img.shields.io/npm/l/@zakodium/utils.svg)](https://github.com/@zakodium/utils/blob/main/LICENSE)
[![test coverage](https://img.shields.io/codecov/c/github/zakodium/utils.svg)](https://app.codecov.io/gh/zakodium/utils)
[![license](https://img.shields.io/github/license/zakodium/utils.svg)](https://github.com/zakodium/utils/blob/main/LICENSE)

Small utilities by zakodium for zakodium.

Expand All @@ -29,20 +29,6 @@ assertDefinedNotNull(value); // throws if value is null or undefined

- [API Reference](https://zakodium.github.io/utils/)

### Assertions

With types mutations.

- `function assert(value: unknown, message?: string | (() => string)): asserts value;`
- `function assertUnreachable(value: never): never;`
- `function assertDefined<T>(value: T): asserts value is Exclude<T, undefined>;`
- `function assertDefinedNotNull<T>(value: T): asserts value is Exclude<T, null | undefined>;`

### Casting

- `function is<Output>(value: unknown, condition: boolean): value is Output;`
- `function cast<Output>(value: unknown): asserts value is Output;`

## Features that could be added later

- Iterable helpers like `map`, `filter` and so on.
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/@zakodium/utils.git"
"url": "git+https://github.com/zakodium/utils.git"
},
"bugs": {
"url": "https://github.com/@zakodium/utils/issues"
"url": "https://github.com/zakodium/utils/issues"
},
"homepage": "https://github.com/@zakodium/utils#readme"
"homepage": "https://github.com/zakodium/utils#readme-ov-file"
}
29 changes: 29 additions & 0 deletions src/functional/identity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { expect, test } from 'vitest';

import { identity } from './identity.ts';

test('should return undefined', () => {
expect(identity(undefined)).toBeUndefined();
});

test('should return null', () => {
expect(identity(null)).toBeNull();
});

test('should return 0', () => {
expect(identity(0)).toBe(0);
});

test('should return false', () => {
expect(identity(false)).toBe(false);
});

test('should return string', () => {
expect(identity('foo')).toBe('foo');
});

test('should return object as-is', () => {
const object = {};

expect(identity(object)).toBe(object);
});
7 changes: 7 additions & 0 deletions src/functional/identity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**

Check warning on line 1 in src/functional/identity.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @returns declaration
* Identity function.
* @param value

Check warning on line 3 in src/functional/identity.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Missing JSDoc @param "value" description
*/
export function identity<T>(value: T): T {
return value;
}
2 changes: 2 additions & 0 deletions src/functional/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './noop.ts';
export * from './identity.ts';
10 changes: 10 additions & 0 deletions src/functional/noop.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, test } from 'vitest';

import { noop } from './noop.ts';

test('noop should always returns undefined', () => {
expect(noop()).toBeUndefined();

// @ts-expect-error check if add truthy arguments continue to return undefined
expect(noop({})).toBeUndefined();
});
4 changes: 4 additions & 0 deletions src/functional/noop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* No operation function.
*/
export function noop() {}

Check warning on line 4 in src/functional/noop.ts

View workflow job for this annotation

GitHub Actions / nodejs / lint-eslint

Unexpected empty function 'noop'
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './types/index.ts';
export * from './functional/index.ts';
Loading