diff --git a/README.md b/README.md index 3d0c5191d..23e142d1f 100644 --- a/README.md +++ b/README.md @@ -377,6 +377,7 @@ average(1, 2, 3); * [`isLowerCase`](#islowercase) * [`isUpperCase`](#isuppercase) * [`mask`](#mask) +* [`pad`](#pad) * [`palindrome`](#palindrome) * [`pluralize`](#pluralize) * [`removeNonASCII`](#removenonascii) @@ -6939,6 +6940,32 @@ mask(1234567890, -4, '$'); // '$$$$567890'
[⬆ Back to top](#table-of-contents) +### pad + +Pads a string on both sides with the specified character, if it's shorter than the specified length. + +Use `String.padStart()` and `String.padEnd()` to pad both sides of the given string. +Omit the third argument, `char`, to use the whitespace character as the default padding character. + +```js +const pad = (str, length, char = ' ') => + str.padStart((str.length + length) / 2, char).padEnd(length, char); +``` + +
+Examples + +```js +pad('cat', 8); // ' cat ' +pad(String(42), 6, '0'); // '004200' +pad('foobar', 3); // 'foobar' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### palindrome Returns `true` if the given string is a palindrome, `false` otherwise. diff --git a/docs/index.html b/docs/index.html index c5b076685..5de07cb15 100644 --- a/docs/index.html +++ b/docs/index.html @@ -74,7 +74,7 @@ document.getElementById('doc-drawer-checkbox').checked = false; } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -1630,6 +1630,11 @@ Foo.prototypeShow examples
mask(1234567890); // '******7890'
 mask(1234567890, 3); // '*******890'
 mask(1234567890, -4, '$'); // '$$$$567890'
+

pad

Pads a string on both sides with the specified character, if it's shorter than the specified length.

Use String.padStart() and String.padEnd() to pad both sides of the given string. Omit the third argument, char, to use the whitespace character as the default padding character.

const pad = (str, length, char = ' ') =>
+  str.padStart((str.length + length) / 2, char).padEnd(length, char);
+
pad('cat', 8); // '  cat   '
+pad(String(42), 6, '0'); // '004200'
+pad('foobar', 3); // 'foobar'
 

palindrome

Returns true if the given string is a palindrome, false otherwise.

Convert string String.toLowerCase() and use String.replace() to remove non-alphanumeric characters from it. Then, String.split('') into individual characters, Array.reverse(), String.join('') and compare to the original, unreversed string, after converting it String.tolowerCase().

const palindrome = str => {
   const s = str.toLowerCase().replace(/[\W_]/g, '');
   return (