diff --git a/README.md b/README.md index 64f30f95d..792d03948 100644 --- a/README.md +++ b/README.md @@ -277,6 +277,7 @@ average(1, 2, 3); * [`byteSize`](#bytesize) * [`capitalize`](#capitalize) * [`capitalizeEveryWord`](#capitalizeeveryword) +* [`decapitalize`](#decapitalize) * [`escapeHTML`](#escapehtml) * [`escapeRegExp`](#escaperegexp) * [`fromCamelCase`](#fromcamelcase) @@ -4139,6 +4140,31 @@ capitalizeEveryWord('hello world!'); // 'Hello World!'
[⬆ Back to top](#table-of-contents) +### decapitalize + +Decapitalizes the first letter of a string. + +Use array destructuring and `String.toLowerCase()` to decapitalize first letter, `...rest` to get array of characters after first letter and then `Array.join('')` to make it a string again. +Omit the `upperRest` parameter to keep the rest of the string intact, or set it to `true` to convert to uppercase. + +```js +const decapitalize = ([first, ...rest], upperRest = false) => + first.toLowerCase() + (upperRest ? rest.join('').toUpperCase() : rest.join('')); +``` + +
+Examples + +```js +decapitalize('FooBar'); // 'fooBar' +decapitalize('FooBar', true); // 'fOOBAR' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### escapeHTML Escapes a string for use in HTML. @@ -5179,6 +5205,7 @@ const httpPost = (url, callback, data = null, err = console.error) => { + const newPost = { "userId": 1, "id": 1337, diff --git a/docs/index.html b/docs/index.html index 46679e732..2dc6237a2 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

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

 

Adapter

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);
+      }

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

 

Adapter

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);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -898,6 +898,10 @@ console.log<
 capitalize('fooBar', true); // 'Foobar'
 

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!'
+

decapitalize

Decapitalizes the first letter of a string.

Use array destructuring and String.toLowerCase() to decapitalize first letter, ...rest to get array of characters after first letter and then Array.join('') to make it a string again. Omit the upperRest parameter to keep the rest of the string intact, or set it to true to convert to uppercase.

const decapitalize = ([first, ...rest], upperRest = false) =>
+  first.toLowerCase() + (upperRest ? rest.join('').toUpperCase() : rest.join(''));
+
decapitalize('FooBar'); // 'fooBar'
+decapitalize('FooBar', true); // 'fOOBAR'
 

escapeHTML

Escapes a string for use in HTML.

Use String.replace() with a regexp that matches the characters that need to be escaped, using a callback function to replace each character instance with its associated escaped character using a dictionary (object).

const escapeHTML = str =>
   str.replace(
     /[&<>'"]/g,
@@ -1160,6 +1164,7 @@ Logs: {
 
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,
diff --git a/snippets/httpPost.md b/snippets/httpPost.md
index 7a52fa2d3..e87361a24 100644
--- a/snippets/httpPost.md
+++ b/snippets/httpPost.md
@@ -26,6 +26,7 @@ const httpPost = (url, callback, data = null, err = console.error) => {
 
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,