3.1.6 • Published 6 months ago

vanillajs-helpers v3.1.6

Weekly downloads
143
License
MIT
Repository
-
Last release
6 months ago

Vanilla JS helpers

This is a collection of simple, no dependency, vanilla JS snippets with the aim of making it easier to work with vanilla JS.

All helpers are written in TypeScript and converted into various JS versions suiting your use case:

  • .js: ES 5+ syntax.
  • .mjs: ES 2020 syntax
  • .cjs: ES 2017 syntax + CommonJS require imports
  • .d.ts: TypeScript description files

BROWSER SPECIFIC HELPERS

These helpers a JS platform agnostic, for browser specific helpers check out: vanillajs-browser-helpers

ES version support

All methods are using latest techniques and generally no efforts have been made to accommodate older browsers which do not support certain features. Polyfills should be used to fill the gap. This is intentional as the need for polyfills are ever diminishing, with modern browsers (and Node environments) getting updated all the time the vast majority of the methods will be supported at one point. Also, with compilers like Babel, polyfills can be included automatically in the build process, making it simple to ensure full support. Therefore it is more future proof and clutter free to leave fallbacks and polyfills out of the methods and just focus on core functionality.

Only in few edge cases where a polyfill might be tricky to implement correctly for a given functionality, fallback functionality will be incorporated.

Note
You will have to add these snippets as part of your transpilation if you wish to have enable auto detection of polyfills.

Available methods

MethodDescription
camelCaseTurn a phrase or word with different casing into a camelCased word.
capitalizeCapitalize Each Word In A Phrase.
chunkStringChop up a string into chunks of the desired length.
currencyFormatFormat a number into a specific currency format like: $ 1.000,00.
dashedTurn a phrase or word with different casing into a dashed-lowercase-phrase.
formatNumberFormat a number according to a given template like: 1.000,00.
hashCreate a unique hash from a string.
hexToNumberConvert Hexadecimal into a number (eg. b4 => 180).
hexToRGBConverts a Hexadecimal color to a RGB(A) color array.
isBooleanTests if a given object is a Boolean
isFunctionTests if a given object is a Function
isGeneratorTests if a given object is a Generator function
isNumberTests if a given object is a Number
isObjectTests if a given object is an Object (plain object)
isStringTests if a given object is a String
leadingZeroMake sure a number is a string of a given length with empty slots filled up with zeroes (0), like: 007
limitDecimalsLimit decimals of a floating number to a given length.
numberToHexConvert a number to a Hexadecimal representation (eg. 180 => b4).
objectTypeDetermine what type a given object is (string, array, etc.)
pascalCaseTurn a phrase or word with different casing into a PascalCased word.
phrasifyConverts a word of a special casing or a phrase, into a space separated phrase.
promisefyConverts a regular method using (err, data) type of callback into a method that returns a promise.
randomHexColorGives a random RGB color.
randomIdGenerates a random id string of a given length.
randomRGBColorGives a random RGB color.
RGBToHexConverts a RGB color to a HEX color (eg. 255, 0, 0 => #ff0000)
safeDateChangeChange to another date without jumping month (eg. if you are going from 31st of January to February you would normally end up in March).
snakeCaseTurn a phrase or word with different casing into a snake_cased word.
truncateEnsures a max length of a given string.
uniqueArrayMake sure an Array only contains unique values.

Installation

npm install vanillajs-helpers
yarn add vanillajs-helpers

Usage

// TypeScript modules
import camelCase from 'vanillajs-helpers/ts/camelCase';
camelCase('Camel cased phrase'); // camelCasedPhrase
// ES 6 Modules
import camelCase from 'vanillajs-helpers/camelCase';
camelCase('Camel cased phrase'); // camelCasedPhrase
// CommonJS Require Modules
const camelCase = require('vanillajs-helpers/cjs/camelCase').default;
camelCase('Camel cased phrase'); // camelCasedPhrase
// ES5 (using CommonJS Require Modules)
const camelCase = require('vanillajs-helpers/es5/camelCase').default;
camelCase('Camel cased phrase'); // camelCasedPhrase

Something missing?

If you have any questions, find any bugs or have ideas for missing functionality you would like to see included, feel free to add an issue in the issue list or perhaps do a Pull Request of a great snippet you created.

3.1.6

6 months ago

3.1.5

6 months ago

3.1.4

6 months ago

3.1.3

2 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.2

5 years ago

2.0.1

6 years ago

1.2.0

7 years ago

1.1.9

7 years ago

1.1.8

7 years ago

1.1.7

7 years ago

1.1.6

7 years ago

1.1.5

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

8 years ago