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

jsmp-infra-useful-snippets-js

v1.1.8

Published

useful js funcction on every day

Downloads

6

Readme

Logo

Just TEST 30 seconds of code mini version

Curated collection of useful JavaScript snippets that you can understand in 20 seconds or less.

  • Use Ctrl + F or command + F to search for a snippet.
  • You can import these snippets into your text editor of choice (VSCode, Atom, Sublime)

Package

You can find a package details npm.

# With npm
npm install jsmp-infra-useful-snippets-js

Usage examples


// ES Modules
import {compact} from 'jsmp-infra-useful-snippets-js';
compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]);

To import snippets with Node:


const compact  = require('jsmp-infra-useful-snippets-js').compact;
compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]);
const  {compact}  = require('jsmp-infra-useful-snippets-js');
compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]);

Table of Contents

📚 Array

📜 String

📚 Array

compact

Removes falsey values from an array.

Use Array.filter() to filter out falsey values (false, null, 0, "", undefined, and NaN).

const compact = arr => arr.filter(Boolean);
compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]); // [ 1, 2, 3, 'a', 's', 34 ]

⬆ Back to top

difference

Returns the difference between two arrays.

Create a Set from b, then use Array.filter() on a to only keep values not contained in b.

const difference = (a, b) => {
  const s = new Set(b);
  return a.filter(x => !s.has(x));
};
difference([1, 2, 3], [1, 2, 4]); // [3]

⬆ Back to top

union

Returns every element that exists in any of the two arrays once.

Create a Set with all values of a and b and convert to an array.

const union = (a, b) => Array.from(new Set([...a, ...b]));
union([1, 2, 3], [4, 3, 2]); // [1,2,3,4]

⬆ Back to top

const users = [{ name: 'fred', age: 48 }, { name: 'barney', age: 36 }, { name: 'fred', age: 40 }];
orderBy(users, ['name', 'age'], ['asc', 'desc']); // [{name: 'barney', age: 36}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]
orderBy(users, ['name', 'age']); // [{name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]

⬆ Back to top

📜 String

capitalizeEveryWord

Capitalizes the first letter of every word in a string.

Use String.replace() to match the first character of each word and String.toUpperCase() to capitalize it.

const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());
capitalizeEveryWord('hello world!'); // 'Hello World!'

⬆ Back to top

reverseString

Reverses a string.

Use the spread operator (...) and Array.reverse() to reverse the order of the characters in the string. Combine characters to get a string using String.join('').

const reverseString = str => [...str].reverse().join('');
reverseString('foobar'); // 'raboof'

⬆ Back to top

truncateString

Truncates a string up to a specified length.

Determine if the string's length is greater than num. Return the string truncated to the desired length, with '...' appended to the end or the original string.

const truncateString = (str, num) =>
  str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
truncateString('boomerang', 7); // 'boom...'

⬆ Back to top

Credits

Icons made by Smashicons from www.flaticon.com is licensed by CC 3.0 BY.