A collection of small JavaScript helper functions for various use cases. All of these little snippets solve problems that are very common, but don't have a widespread decent implementation. Most functions are available as easy-to-use prototypes on their respective objects.
Rounds a number to a given precision and omits trailing 0 values.
100.51235.round(3)
100.512
Gets only the base of a given URL.
'https://www.twitter.com/RadLikeWhoa_'.base()
'twitter.com'
Format a string using placeholders like {0}
.
'{0}, hello {1} world!'.format('Oh', 'beautiful')
'Oh, hello beautiful world!'
Insert a string into another one at a given numerical index or a keyword.
'I am here.'.insert('in between ', 5)
'I am in between here.'
Truncates the string to the given length and replaces superfluous characters with the given substitute.
'This is a longer text'.truncate(15)
'This is a long…'
Count the words inside a given element. The output can be formatted using the {{count}}
placeholder.
'Hello, world!'.words()
2
Returns the estimated time to complete reading of a text in a human readable format.
readingtime(2720, 'Reading this takes {time}.', 270)
'Reading this takes about 10 minutes.'