Some JS utils we frequently use in our projects.
Install using npm:
npm install --save @pixelherz/js-utils
For usage examples see section Utilities below.
Returns string values from a given array as space separated string to be used as class names. Empty items in the array are ignored.
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>
Returns a random item from a given array
import { randItem } from '@pixelherz/js-utils/array'
randItem(['a', 'b', 'c'])
Prints a project signature to the console. Pass data from package.json
or a custom data object.
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: '[email protected]',
url: 'https://author.url',
},
}
signConsole(projectInfo)
Utils tailored to work with Prismic.
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.
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',
}]
*/
Returns a hash from a given string
import { strToHash } from '@pixelherz/js-utils/string'
strToHash('foo') // 101574
Truncates a given string to a given max length and appends a given string at the end.
import { wordbreakTruncate } from '@pixelherz/js-utils/string'
wordbreakTruncate('the quick brown fox jumps over', 24, '…')
// the quick brown fox…
We use SemVer for versioning. For the versions available, see the tags on this repository.
This project is licensed under the ISC License - see the LICENSE.md file for details.