Socket
Socket
Sign inDemoInstall

my-easy-fp

Package Overview
Dependencies
0
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

my-easy-fp

Simple functional programming utility & Misc programming tool


Version published
Maintainers
1
Weekly downloads
24,977
decreased by-7.18%

Weekly downloads

Readme

Source

Simple functional programming utility & programming helper tools

Download Status Github Star Github Issues NPM version License

Simple utility functions that can use browser, node.

Function list

boolean

namedescription
isFalseIf argument false, return true
isTrueIf argument true, return true
invertreturn inverted boolean value

array

namedescription
populatepopulate array zero base, if you pass second argument true that populate one base
chunkarray split given size
lastpick last element from array
firstpick first element from array
toArraymake array given argument
settifymake it set and convert it back to array

empty

namedescription
isUndefinedif argument undefiend, return true
isNotUndefinedif argument not undefined, return true
isNullif argument null, return true
isNotNullif argument not null, return true
isNotEmptyif argument not null and undefined, return true
isEmptyif argument null or undefined, return true
isComplexEmptyif argument not undefined and null, do additional test isNaN, empty string, empty array, empty object
isNotComplexEmptyreturn inverted value isComplexEmpty

misc

namedescription
typedkeysame work Object.keys, but typed key in Recoed
getRandomRangereturn random value in min and max
getRandomRangeIntreturn random integer value in min and max
isErrorif argument is Error return Error or return undefined

sleep

namedescription
sleepsleep given millisecond

Custom Utility Types

namedescription
TResolvePromiseget type resolved promise
TResolveArrayget type resolve array
TNullablePickconvert specific field to nullable
TNonNullableObjectobject type each field to non nullable
TNonNullablePickconvert specific field to non nullable

False & True checker

Why need this utility?

import { isFalse } from 'my-easy-fp';

const iamboolean = false;

// this line easily miss in refactoring or changing
if (!iamboolean) {
  console.log('I am execute false-case');
}

// more than better!
if (isFalse(iamboolean)) {
  console.log('I am execute false-case');
}

Empty checker

Why need this utility?

const iamempty?: string | null = undefined;

// this line some boring task
if (iamempty === undefined || iamempty === null || iamempty !== '') {
  console.log('i am empty');
}

// more than better!
if (isComplexEmpty(iamempty)) {
  console.log('i am empty');
}

You need only undefined and null check, use double equal operator.

const iamempty?: string | null = undefined;

// Recently ESLint allow double equal for null check
// see https://eslint.org/docs/latest/rules/eqeqeq#allow-null
if (iamempty == null) {
  console.log('i am empty');
}

Sleep

Why need this utility?

const ms = 1000;

// this line some boring task
await new Promise((resolve) => setTimeout(() => resolve(), ms));

// more than better!
await sleep(ms);

Array

populate, chunk utility here.

// seq is [0,1,2,3,4,5,6,7,8,9]
const seq = populate(10);

// seq is [1,2,3,4,5,6,7,8,9,10]
const seq = populate(10, true);

// chunked is [[1,2],[3,4],[5,6],[7]]
const chunked = chunk([1, 2, 3, 4, 5, 6, 7], 2);

// settify is [ 1, 2, 3, 4 ]
const settified = settify([1, 1, 2, 3, 2, 3, 4]);

// zeroIndex is 1
const zeroIndex = atOrThrow([1, 2, 3], 0);

Type Helper

resolve promise, array. But you can use type-fest or ts-essentials. Recommand use type-fest and tsessentials.

// you can get string | Buffer type
type TResolvedPromiseType = TResolvePromise<ReturnType<typeof fs.promises.readFile>>;

// you can get number type
type TResolvedArrayType = TResolveArray<number[]>;

interface IStudent {
  name: string;
  major: string;
  grade: number;
}

// you can get { name?: string; major?: string; grade: number; }
// converted Omit<IStudent, 'name' | 'major'> & Pick<Partial<IStudent>, 'name' | 'major'>
type TCliArgumentStudent = TNullablePick<IStudent, 'name' | 'major'>;

FAQs

Last updated on 07 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc