# zshared

> useful common javascript/typescript utilities I collected over the years

Latest version **1.0.11** (published 2020-09-27) · ISC license · 0 weekly downloads

## Install

```sh
npm install zshared
pnpm add zshared
yarn add zshared
bun add zshared
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.11 |
| Published | 2020-09-27 |
| First published | 2020-01-04 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 59.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | zohar1000 |

## Links

- npm: https://www.npmjs.com/package/zshared
- Repository: https://github.com/zohar1000/zshared
- Issues: https://github.com/zohar1000/zshared/issues
- npm.io page: https://npm.io/package/zshared

## Recent versions

- 1.0.11 (latest) — 2020-09-27
- 1.0.9 — 2020-06-24
- 1.0.6 — 2020-06-23
- 1.0.5 — 2020-06-19
- 1.0.4 — 2020-06-19
- 1.0.2 — 2020-05-01
- 1.0.1 — 2020-04-22
- 1.0.0 — 2020-01-04

## README

# zshared
Useful javascript/typescript utilities collected over the years.<br/>
The utilities relate to handling arrays, objects, strings, time, etc.<br/>
It can be used on browser/node/deno, it has 0 dependencies and is optimized for performance.

### Installation
```sh
npm install zshared
```

The library can be consumed by using either 'require' or 'import' syntax.<br/>
The libraray is devided to classes, each handles its own area (arrays, strings, etc).<br/>
Each class name begins with 'Z' and the area (ZArray, ZString, etc).<br/>
```sh
import { ZTime, ZArray } from 'zshared';
```


## Some examples
Below are some examples, the complete list can be found in the docs.<br/>
All class functions are static.

#### Time
```sh
await ZTime.sleep(1000);  // wait 1 second
```

get local/utc time in universal format, local time zone GMT+3
```sh
ZTime.utcUniDateTime();     // returns '2020-04-29 17:29:20'
ZTime.localUniDateTime();   // returns '2020-04-29 20:29:20'
```

convert seconds to display time (hh:mm:ss) 
```sh
ZTime.seconds2UniTime(3500) // returns '33:20'
```

#### Objects
```sh
const obj1 = { a: 1, b: 2, c: 3 };
ZObj.clone(obj1);  // returns shallow copy { a: 1, b: 2, c: 3 };
ZObj.clone(obj1, ['a', 'c']);  // pass keys array, returns: { a: 1, c: 3 };
```

```sh
ZObj.areEquals(obj1, { a: 1, b: 2 });  // returns false
ZObj.areEquals(obj1, { a: 1, b: 2, c: 3 });  // returns true
```

#### Arrays
```sh
items = ['a', 'b', 'a', 'c'];
ZArray.distincts(items);  // returns ['a', 'b', 'c'];
ZArray.deleteItem(items, 'b');  // items is now ['a', 'a', 'c'];
```

```sh
items = ['a', 'b', 'c'];
ZArray.toObj(items);  // returns { a: 'a', b: 'b', c: 'c' };
ZArray.toObj(items, item => '_' + item);  // pass a function, returns { a: '_a', b: '_b', c: '_c' };
```

convert items array to objects
```sh
items = [{ a: 1 }, { b: 2 }]; 
ZArray.toObj(items);  // returns { a: 1, b: 2 };
ZArray.toObj(items, (key, value) => value * 2);  // pass a function, returns { a: 2, b: 4 };
```

#### Numbers
```sh
ZNumber.thousandsSep(12345);  // returns 12,345 or 12.345, depends on locale
```

#### Strings
```sh
const str = '2 cats met another cat';
ZString.replaceAll(str, 'cat', 'dog');   // returns '2 dogs met another dog'
ZString.occurrences(str, 'cat');         // returns 2
ZString.initialCapital('good morning');  // returns 'Good morning'
ZString.replaceParams('I say {0} {1} and {0}', 'bla', 'gla);  // returns 'I say bla gla and bla'
```

#### logt
the libraray contains also a static function 'logt', it acts like console.log() with a time prefix
```sh
logt('some message', 1200, 'another message');  // output: 2020-04-29 23:39:12.397 ==> some message 1200 another message
```

---
_Source: https://npm.io/package/zshared · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
