1.1.8 • Published 6 years ago

jsmp-infra-useful-snippets-js v1.1.8

Weekly downloads
13
License
ISC
Repository
github
Last release
6 years ago

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.

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago