# @iimm/shared

> shared utils on browser and react env

Latest version **2.5.0** (published 2024-04-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @iimm/shared
pnpm add @iimm/shared
yarn add @iimm/shared
bun add @iimm/shared
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.5.0 |
| Published | 2024-04-19 |
| First published | 2022-12-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 157.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | liudichen |
| Keywords | shared, nanoid, color, iimm |

## Links

- npm: https://www.npmjs.com/package/@iimm/shared
- npm.io page: https://npm.io/package/@iimm/shared

## Dependencies (4)

- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [nanoid](https://npm.io/package/nanoid.md) ^3.3.7
- [lodash-es](https://npm.io/package/lodash-es.md) ^4.17.21
- [htmlparser2](https://npm.io/package/htmlparser2.md) ^8.0.2

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 2.5.0 (latest) — 2024-04-19
- 2.4.2 — 2024-04-19
- 2.4.1 — 2023-07-05
- 2.4.0 — 2023-05-06
- 2.3.0 — 2023-03-10
- 2.2.0 — 2023-03-10
- 2.1.2 — 2023-03-06
- 2.1.1 — 2023-03-06
- 2.1.0 — 2023-03-03
- 2.0.0 — 2023-03-03
- 1.3.2 — 2023-03-01
- 1.3.1 — 2023-02-24
- 1.3.0 — 2023-02-24
- 1.2.1 — 2023-01-11
- 1.2.0 — 2023-01-11
- … 10 more at https://npm.io/package/@iimm/shared/versions

## README

# @iimm/shared

[![NPM version](https://img.shields.io/npm/v/@iimm/shared.svg?style=flat)](https://npmjs.org/package/@iimm/shared)
[![NPM downloads](http://img.shields.io/npm/dm/@iimm/shared.svg?style=flat)](https://npmjs.org/package/@iimm/shared)

## Install

```bash
npm install
```

## 导出内容

### nanoid 
```typescript
const nanoid: (size?: number = 21) => string
const customNanoidAlphabet: (alpha: string, defaultSize?: number = 21) => ((size?: number = defaultSize) => string)
```

#### file

```typescript
// ===== convert =====
const fileToBase64 = (file: File) => Promise<string>;
const fileToArrayBuffer = (file: File) => Promise<ArrayBuffer>;
const fileToBinaryString = (file: File) => Promise<string>;
const fileToText = (file: File, encoding = 'utf-8') => Promise<string>;

// ==== info =====
const getMimeTypeFromBase64 = (base64Str: string) => mimeType:string;

// ==== download ====
const generateFileDownload = (file: InputFile, fileName = '', extension = '', onDownloadSuccess?: (file: InputFile, fileName: string) => void, onDownloadFail?: (file: InputFile, fileName: string) => void) => void;
```

#### image

```typescript
const isImageMimeType = (type: string) => type.indexOf('image/') === 0;
```

#### url

```typescript
const getFileNameFromUrl = (url = '') => string;
const getFileExtensionFromUrl = (url = '') => string;
```

### isX

```typescript
const isEqual = (a?: any, b?: any) => [ 'number', 'string' ].includes(typeof (a ?? 0))
  ? a === b
  : JSON.stringify(a) === JSON.stringify(b);

const isInArray = (value: any, array: any[]) => {
  if (!array || !Array.isArray(array)) return false;
  return [ 'number', 'string' ].includes(typeof (value ?? 0))
    ? array.includes(value)
    : array.some(item => isEqual(value, item));
};

const isObject = (value: any) => value !== null && typeof value === 'object';
const isFunction = (value: any) => typeof value === 'function';
const isString = (value: any) => typeof value === 'string';
const isNumber = (value: any) => typeof value === 'number';
const isUndefined = (value: any) => typeof value === 'undefined';


const isBrowser = !!(typeof window !== 'undefined' && window.document && window.document.createElement);

/** 字符串是否是Base64，可以有data前缀，也可以无 */
const isBase64 = (str: string) => {
  if (!str) return false;
  const re = /^(data:([a-z0-9/+.-])+;base64,)?([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/;
  return re.test(str);
};
```

TODO

## LICENSE

MIT

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