Skip to content

wangweiwei/utils

Repository files navigation

logo

Utils

Codecov Coverage Build Status Latest Version on NPM Languages Code Size Languages Count
Downloads Contributors Anon Issue Software License

Some convenience utils.

Installation

npm install --save @dengfengwang/utils

or

yarn add @dengfengwang/utils

Usage

You can import(or require) @dengfengwang/utils by getting it from npm

import { $number } from '@dengfengwang/utils';

const { toFixed } = $number;

console.log(toFixed(1.123, 2))

Table of contents

API

$array

.reverse

Reverses an array in place.

Params

  • arr {Array}: The array to reverse.
  • start {Number}: Starting index.
  • end {Number}: Ending index.

Example

import { $array } from '@dengfengwang/utils';
const { reverse } = $array;

const arr = [1, 2, 3];
reverse(arr, 0, arr.length - 1);
console.log(arr);
//=> [3,2,1]

$boolean

.random

Get a random boolean (true/false).

Params

  • returns {Boolean}: A boolean (true or false).

Example

import { $boolean } from '@dengfengwang/utils';
const { random } = $boolean;

console.log(random());
//=> true/false

$date

.isWeekday

Check if the provided day is a weekday.

Params

  • date {Date}: Provided Date.
  • returns {Boolean}: Either a weekday or not(a weekend day).

Example

import { $date } from '@dengfengwang/utils';
const { isWeekday } = $date;

console.log(isWeekday(new Date('2021-05-17T00:00:00')));
//=> true

$math

.average

Get average value of arguments.

Params

  • ...args {Arguments}: Some number arguments.
  • returns {Number}: Average value of the arguments.

Example

import { $math } from '@dengfengwang/utils';
const { average } = $math;

console.log(average(1,2,3));
//=> 2

$number

.isEven

Check if a number is even or odd.

Params

  • num {Number}: Arbitrary integer.
  • returns {Boolean}: Even or not(odd).

Example

import { $number } from '@dengfengwang/utils';
const { isEven } = $number;

console.log(isEven(2));
//=> true
console.log(isEven(3));
//=> false

.toFixed

Truncate a number to a fixed decimal point.

Params

  • num {Number}: The needed formatted number.
  • digits {Number}: The number of digits to appear after the decimal point.
  • returns {Boolean}: The given number using fixed-point notation.

Example

import { $number } from '@dengfengwang/utils';
const { toFixed } = $number;

console.log(toFixed(2.123, 2));
//=> 2.12

$string

.reverse

Reverse the characters in a string.

Params

  • str {String}: The string to reverse.
  • returns {String} The reversed string.

Example

import { $string } from '@dengfengwang/utils';
const { reverse } = $string;

console.log(reverse("abc"));
//=> 'cba'

License

Software License

Copyright (c) 2021-present, Weiwei Wang