Skip to content

Some JS utils we frequently use in our projects.

License

Notifications You must be signed in to change notification settings

Pixelherz/js-utils

Folders and files

NameName
Last commit message
Last commit date
May 18, 2020
Sep 17, 2018
Aug 31, 2018
Aug 4, 2020
Aug 4, 2020
Aug 31, 2018
Apr 24, 2020
Sep 17, 2018
Sep 17, 2018
Sep 28, 2018
Mar 9, 2023
May 18, 2020
Apr 24, 2020
Apr 21, 2020

Repository files navigation

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 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.

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

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.

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)

Prismic

Utils tailored to work with Prismic.

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.

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

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.

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

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

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