@fastkit/helpers
Version:
A small collection of helper implementations for processing primitive values and objects.
218 lines (152 loc) ⢠5.2 kB
Markdown
//github.com/dadajam4/fastkit/blob/main/packages/helpers/README-ja.md)
A collection of small helper implementations for processing primitive values and objects.
- **Full TypeScript Support**: Provides type-safe operations with strict type definitions
- **Lightweight**: Minimized size with minimal necessary dependencies
- **Multi-functional**: Supports a wide range of data types including strings, arrays, objects, and numbers
- **Practical**: Functions commonly used operations in actual development
- **Zero Dependencies**: Can be used standalone without depending on external libraries
```bash
npm install @fastkit/helpers
pnpm add @fastkit/helpers
```
```typescript
import {
capitalize,
toHalfWidth,
stripIndent,
removeSpace
} from '@fastkit/helpers';
// Capitalize the first character
capitalize('helloWorld'); // ā 'HelloWorld'
// Convert full-width characters to half-width
toHalfWidth('H$LLOćWORLD'); // ā 'HELLO WORLD'
// Normalize indentation
const code = stripIndent(`
const message = 'Hello';
console.log(message);
`);
// Remove spaces
removeSpace('Hello World'); // ā 'HelloWorld'
```
```typescript
import {
arrayUnique,
arrayRemove,
range,
flattenRecursiveArray
} from '@fastkit/helpers';
// Remove duplicates
arrayUnique([1, 2, 2, 3, 3]); // ā [1, 2, 3]
// Remove element (modifies original array)
const arr = [1, 2, 3];
arrayRemove(arr, 2); // arr becomes [1, 3]
// Generate range array
range(3); // ā [0, 1, 2]
range(3, 5); // ā [5, 6, 7]
// Flatten recursive array
flattenRecursiveArray([1, [2, [3, 4]]]); // ā [1, 2, 3, 4]
```
```typescript
import {
isPlainObject,
pickProperties,
omitProperties,
removeUndef,
mixin
} from '@fastkit/helpers';
// Check if plain object
isPlainObject({}); // ā true
isPlainObject(new Date()); // ā false
// Select properties
const obj = { a: 1, b: 2, c: 3 };
pickProperties(obj, ['a', 'c']); // ā { a: 1, c: 3 }
// Exclude properties
omitProperties(obj, ['b']); // ā { a: 1, c: 3 }
// Remove undefined properties
removeUndef({ a: 1, b: undefined, c: 3 }); // ā { a: 1, c: 3 }
// Mixin objects
const base = { a: 1 };
const trait = { b: 2 };
const mixed = mixin(base, trait); // { a: 1, b: 2 }
```
```typescript
import {
isEmpty,
notEmptyValue,
inNonNullable,
toNumber
} from '@fastkit/helpers';
// Empty value check
isEmpty(''); // ā true
isEmpty([]); // ā true
isEmpty(null); // ā true
isEmpty('hello'); // ā false
// Get first non-empty value
notEmptyValue(['', null, 'hello']); // ā 'hello'
// null/undefined check
inNonNullable(value); // value is not null | undefined
// Number conversion
toNumber('123'); // ā 123
toNumber('123.45'); // ā 123.45
```
Capitalizes the first character of a string
Uncapitalizes the first character of a string
Converts full-width characters to half-width
Removes all spaces and tab characters
Normalizes indentation by removing the minimum indent count
Returns array with duplicates removed
Removes specified element from array (destructive operation)
#### `range(length: number, offset?: number): number[]`
Generates numeric array of specified range
#### `flattenRecursiveArray<T>(source: RecursiveArray<T>): T[]`
Flattens recursive array
### Object Operations
#### `isPlainObject<T>(value: unknown): value is T`
Determines whether value is a plain object
#### `isObject<T>(value: unknown): value is T`
Determines whether value is an instance derived from Object class
#### `pickProperties<T, K>(obj: T, props: K[]): Pick<T, K>`
Extracts only specified properties
#### `omitProperties<T, K>(obj: T, props: K[]): Omit<T, K>`
Excludes specified properties
#### `mixin<T, U>(base: T, trait: U): Mixin<T, U>`
Generates a Proxy that mixes objects
### Number Operations
#### `toInt(value: string | number): number`
Converts to integer value
#### `toFloat(value: string | number): number`
Converts to floating-point number
#### `toNumber(source: any): number`
Normalizes to number
### Utilities
#### `isEmpty(value: any): boolean`
Determines whether value is empty
#### `notEmptyValue<T>(args: T[], defaultValue?: T): T`
Returns first non-empty value from array
#### `inNonNullable<T>(value: T): value is Exclude<T, null | undefined>`
Determines whether value is not null or undefined
## Dependencies
- `@fastkit/ts-type-utils`: TypeScript type utilities (within same repository)
## Documentation
For detailed documentation, please visit [here](https://dadajam4.github.io/fastkit/helpers/).
## License
MIT
š English | [ę„ę¬čŖ](https: