# @pixelherz/js-utils

> Some JS utils we frequently use in our projects.

Latest version **1.3.2** (published 2020-05-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install @pixelherz/js-utils
pnpm add @pixelherz/js-utils
yarn add @pixelherz/js-utils
bun add @pixelherz/js-utils
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.2 |
| Published | 2020-05-18 |
| First published | 2018-08-31 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | René Keller |
| Maintainers | rekomat |
| Keywords | js, utils |

## Links

- npm: https://www.npmjs.com/package/@pixelherz/js-utils
- Repository: https://github.com/Pixelherz/js-utils
- Homepage: https://github.com/Pixelherz/js-utils#readme
- Issues: https://github.com/Pixelherz/js-utils/issues
- npm.io page: https://npm.io/package/@pixelherz/js-utils

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.3.2 (latest) — 2020-05-18
- 1.3.1 — 2020-05-13
- 1.3.0 — 2020-04-24
- 1.2.1 — 2020-04-21
- 1.2.0 — 2020-04-21
- 1.1.2 — 2018-09-28
- 1.1.1 — 2018-09-28
- 1.1.0 — 2018-09-28
- 1.0.1 — 2018-09-17
- 1.0.0 — 2018-08-31

## README

# js-utils

Some JS utils we frequently use in our projects.


## Getting Started

### Installing

Install using npm:

```
npm install --save @pixelherz/js-utils
```

For usage examples see section [Utilities](#utilities) below. 


## Utilities

### Array

#### arrToClassName

Returns string values from a given array as space separated string to be used as class names. Empty items in the array are ignored.

```js
import { arrToClassName } from '@pixelherz/js-utils/array'
// Usage sample taken from a react component
const hostClasses = ['host', isActive && 'host--active']
<div className={arrToClassName(hostClasses)}></div>
```

#### randItem

Returns a random item from a given array

```js
import { randItem } from '@pixelherz/js-utils/array'
randItem(['a', 'b', 'c'])
```

### Fun

#### signConsole

Prints a project signature to the console. Pass data from `package.json` or a custom data object. 

```js
import { signConsole } from '@pixelherz/js-utils/fun'
// Pass data from package.json..
signConsole(require('./package.json'))
// .. or use a custom data object
const projectInfo = {
  name: 'project name',
  version: '1.4.1',
  description: 'project description',
  author: {
    name: 'author name',
    email: 'author@email.com',
    url: 'https://author.url',
  },
}
signConsole(projectInfo)
```

### Prismic

Utils tailored to work with [Prismic](https://prismic.io/).

#### wordbreakTruncateRichText

Truncates a given Prismic richtext to a given max length and appends a given string at the end.  
**NOTE:** Only the first element in the richtext object is truncated and returned.

```js
import { wordbreakTruncateRichText } from '@pixelherz/js-utils/prismic'
const prismicRichText = [{
  spans: [
    { start: 2, end: 5, type: 'strong' },
    { start: 8, end: 25, type: 'strong' },
    { start: 26, end: 30, type: 'strong' },
  ],
  text: 'the quick brown fox jumps over the lazy dog.',
  type: 'paragraph',
}]
wordbreakTruncateRichText(prismicRichText, 18, '…')
/*
[{
  spans:[
    { start: 2, end: 5, type: 'strong' },
    { start: 8, end: 15, type: 'strong' },
  ]
  text: 'the quick brown…',
  type: 'paragraph',
}]
*/

```


### String

#### strToHash

Returns a hash from a given string

```js
import { strToHash } from '@pixelherz/js-utils/string'
strToHash('foo') // 101574
```

#### wordbreakTruncate

Truncates a given string to a given max length and appends a given string at the end.

```js
import { wordbreakTruncate } from '@pixelherz/js-utils/string'
wordbreakTruncate('the quick brown fox jumps over', 24, '…') 
// the quick brown fox…
```


## Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/Pixelherz/js-utils/tags). 


## License

This project is licensed under the ISC License - see the [LICENSE.md](LICENSE.md) file for details.

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