npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@graubnla/helpers

v6.0.0

Published

[![npm](https://img.shields.io/npm/v/@graubnla/helpers.svg)](https://www.npmjs.com/package/@graubnla/helpers) [![Travis](https://img.shields.io/travis/lgraubner/helpers.svg)](https://travis-ci.org/lgraubner/helpers)

Downloads

27

Readme

JavaScript helper functions

npm Travis

Loose collection of helper functions with expressiveness and minimal overhead in mind.

Table of contents

General

basename(str[, sep])

Returns string

Returns basename of a path.

import basename from '@graubnla/helpers/basename';

const path = 'this/is/a/path';

basename(path); // "path"

cloneArray(arr)

Returns Array

Clones an array.

import cloneArray from '@graubnla/helpers/cloneArray';

const arr = ['monkey', 'lion'];

const clonedArr = cloneArray(arr);

cloneObject(obj)

Returns Object

Clones an object.

import cloneObject from '@graubnla/helpers/cloneObject';

const obj = { animal: 'monkey' };

const clonedObj = cloneObject(obj);

formatNumber(num[, precision, decimal, thousand])

Returns string

Formats number.

import formatNumber from '@graubnla/helpers/formatNumber';

const num = 3256.1415;

console.log(formatNumber(num, 2, ',', '.')); // "3.256,14"

getFileExtension(filename)

Returns string

Extracts file extension from filename string. Also works with path and url.

import getFileExtension from '@graubnla/helpers/getFileExtension';

const ext = getFileExtension('image.png');
console.log(ext); // "png"

isObject(object)

Returns boolean

Detect if given value is an object.

import isObject from '@graubnla/helpers/isObject';

console.log(isObject({})); // true

mergeDeep(target, source)

Returns Object

Deep merges two objects. Mutates the target object.

import mergeDeep from '@graubnla/helpers/mergeDeep';

const target = { foo: { bar: 'baz' } };
const source = { foo: { bar: 2 }, num: 3 };

console.log(mergeDeep(target, source)); // { foo: { bar: 2 }, num: 3 }

noop()

Returns undefined

No operation function. Does nothing.

import noop from '@graubnla/helpers/noop';

noop(); // undefined

pipe(fn1, fn2[, fn3, ...])

Pipe argument through multiple functions.

import pipe from '@graubnla/helpers/pipe';

const process = pipe(fn1, fn2, fn3);
const result = process(arg);

prettyJSON(obj)

Returns string

Pretty prints JSON string.

import prettyJSON from '@graubnla/helpers/prettyJSON';

const obj = {
  animal: 'monkey',
  num: 2
};

console.log(prettyJSON(obj));
// "{
//   "animal": "monkey",
//   "num": 2
// }"

random(min, max)

Returns number

Creates random number between (including) min and max.

import random from '@graubnla/helpers/random';

console.log(random(1, 100)); // random

removeTrailingSlash(str)

Returns string

Removes trailing slashes from a string.

import removeTrailinSlash from '@graubnla/helpers/removeTrailinSlash';

const newStr = removeTrailingSlash('/animal/monkey/');
console.log(newStr); // "/animal/monkey"

round(num[, precision])

Returns number

Round numbers with given precision.

import round from '@graubnla/helpers/round';

console.log(round(3.1415, 2)); // 3.14

stripTags(str)

Returns string

Strip all html like tags from string.

import stripTags from '@graubnla/helpers/stripTags';

const str = '<p>I like monkeys.</p>';

console.log(stripTags(str)); // I like monkeys.

percentage(num)

Returns string

Generate percentage string of given input.

import percentage from '@graubnla/helpers/percentage';

const num = 0.12;

console.log(percentage(num)); // "12%"

slug(str)

Returns string

Create slug from string. Transforms to lower-case, remove whitespace, and special chars.

import slug from '@graubnla/helpers/slug';

const str = 'I like monkeys.';

console.log(slug(str)); // i-like-monkeys

timestamp()

Returns number

Creates a millisecond timestamp of now.

import timestamp from '@graubnla/helpers/timestamp';

console.log(timestamp()); // 1513173869345

DOM

$(str)

Returns Node

Shortcut for document.querySelector. Returns first matching element or null.

import $ from '@graubnla/helpers/dom/$';

const el = $('.element');

$$(str)

Returns NodeList

Shortcut for document.querySelectorAll.

import $$ from '@graubnla/helpers/dom/$$';

const els = $$('.element');

offset(el)

Return Object

Returns top and left offset of an element relative to the document.

import offset from '@graubnla/helpers/dom/offset';

const el = document.querySelector('#el');
console.log(offset(el)); // { top: 123, left: 456 }

onReady(cb)

Executes callback on document ready.

import onReady from '@graubnla/helpers/dom/onReady';

onReady(() => {
  // document ready
});

scrollTo(dest[, duration, easing])

Scrolls view to specified y value or element position. Accepts duration parameter in milliseconds and an alternate easing function. Uses requestAnimationFrame for performant animation.

import scrollTo from '@graubnla/helpers/dom/scrollTo';

const el = document.getElementById('element');
scrollTo(el);

// scroll to top
scrollTo(0, 500);

License

MIT © Lars Graubner