Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/keng42/js-ext

Some commonly used js functions
https://github.com/keng42/js-ext

Last synced: 1 day ago
JSON representation

Some commonly used js functions

Awesome Lists containing this project

README

        

# js-ext

Some commonly used js functions

1. Safe floating point number calculation
2. Determine if a variable is of a certain type
3. Outputs formatted date strings in China Standard Time regardless of the current time zone
4. Wrap setTimeount into a sleep function

## Usage

```ts
import jsExt from 'js-ext';

// number
jsExt.pad(6); // '06'
jsExt.pad(44); // '44'
jsExt.pad(89, 4); // '0089'

jsExt.safeAdd(0.1, 0.2); // 0.3
jsExt.safeSub(0.3, 0.2); // 0.1
jsExt.safeMul(0.1, 0.2); // 0.02
jsExt.safeDiv(0.3, 0.1); // 3

// type
jsExt.isArray([1]);
jsExt.isBoolean(false);
jsExt.isDate(new Date());
jsExt.isError(new Error(''));
jsExt.isFunction(() => {});
jsExt.isNull(null);
jsExt.isNumber(0);
jsExt.isRegExp(/\d+/);
jsExt.isString('0');
jsExt.isUndefined(undefined);

// date
const da = new Date('1989-04-05T12:02:03Z');

jsExt.toChinaTimeString(da); // '20:02:03'
jsExt.toChinaDateString(da); // '1989-04-05 20:02:03'
jsExt.toChineseDateString(da); // '1989年04月05日 20:02:03'

jsExt.ts(da); // '19890405200203'
jsExt.ts8(da); // '19890405'
jsExt.ts6(da); // '200203'

// sleep
(async () => {
await jsExt.sleep(1000, 'say hi');
})();
```

### Browser

```html

const jsExt = require('js-ext');

console.log(jsExt.ts(new Date()));

```