diff --git a/README.md b/README.md index 2ee4f93dc..2e86c5224 100644 --- a/README.md +++ b/README.md @@ -1274,7 +1274,7 @@ const partition = (arr, fn) => Examples ```js -var users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }]; +const users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }]; partition(users, o => o.active); // [[{ 'user': 'fred', 'age': 40, 'active': true }],[{ 'user': 'barney', 'age': 36, 'active': false }]] ``` @@ -3311,7 +3311,7 @@ const luhnCheck = num => { ```js luhnCheck('4485275742308327'); // true -luhnCheck(6011329933655299); // true +luhnCheck(6011329933655299); // false luhnCheck(123456789); // false ``` @@ -3508,7 +3508,7 @@ const sdbm = str => { Examples ```js -console.log(sdbm('name')); // -3521204949 +sdbm('name'); // -3521204949 ``` @@ -3910,7 +3910,7 @@ const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]); Examples ```js -objectToPairs({ a: 1, b: 2 }); // [['a',1],['b',2]]) +objectToPairs({ a: 1, b: 2 }); // [['a',1],['b',2]] ``` @@ -4340,7 +4340,7 @@ const mask = (cc, num = 4, mask = '*') => ```js mask(1234567890); // '******7890' mask(1234567890, 3); // '*******890' -mask(1234567890, -4, '$'); // '1234$$$$$$' +mask(1234567890, -4, '$'); // '$$$$567890' ``` @@ -4476,7 +4476,7 @@ const splitLines = str => str.split(/\r?\n/); Examples ```js -splitLines('This\nis a\nmultiline\nstring.\n'); // ['This', 'is a', 'multiline', 'string' , ''] +splitLines('This\nis a\nmultiline\nstring.\n'); // ['This', 'is a', 'multiline', 'string.' , ''] ``` @@ -4830,7 +4830,7 @@ isNumber(1); // true ### isPrimitive -Returns a boolean determining if the supplied value is primitive or not. +Returns a boolean determining if the passed value is primitive or not. Use `Array.includes()` on an array of type strings which are not primitive, supplying the type using `typeof`. @@ -5155,9 +5155,9 @@ const prettyBytes = (num, precision = 3, addSpace = true) => { Examples ```js -prettyBytes(1000); // 1 KB -prettyBytes(-27145424323.5821, 5); // -27.145 GB -prettyBytes(123456789, 3, false); // 123MB +prettyBytes(1000); // "1 KB" +prettyBytes(-27145424323.5821, 5); // "-27.145 GB" +prettyBytes(123456789, 3, false); // "123MB" ``` @@ -5251,7 +5251,7 @@ const toDecimalMark = num => num.toLocaleString('en-US'); Examples ```js -toDecimalMark(12305030388.9087); // "12,305,030,388.9087" +toDecimalMark(12305030388.9087); // "12,305,030,388.909" ``` diff --git a/docs/index.html b/docs/index.html index c10837337..78f476278 100644 --- a/docs/index.html +++ b/docs/index.html @@ -218,7 +218,7 @@ Object.assig }, [[], []] ); -
var users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }];
+
const users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }];
 partition(users, o => o.active); // [[{ 'user': 'fred',    'age': 40, 'active': true }],[{ 'user': 'barney',  'age': 36, 'active': false }]]
 

pick

Picks the key-value pairs corresponding to the given keys from an object.

Use Array.reduce() to convert the filtered/picked keys back to an object with the corresponding key-value pair if the key exists in the obj.

const pick = (obj, arr) =>
   arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
@@ -731,7 +731,7 @@ own individual rating by supplying it as the third argument.
   return sum % 10 === 0;
 };
 
luhnCheck('4485275742308327'); // true
-luhnCheck(6011329933655299); //  true
+luhnCheck(6011329933655299); //  false
 luhnCheck(123456789); // false
 

median

Returns the median of an array of numbers.

Find the middle of the array, use Array.sort() to sort the values. Return the number at the midpoint if length is odd, otherwise the average of the two middle numbers.

const median = arr => {
   const mid = Math.floor(arr.length / 2),
@@ -766,7 +766,7 @@ own individual rating by supplying it as the third argument.
     0
   );
 };
-
console.log(sdbm('name')); // -3521204949
+
sdbm('name'); // -3521204949
 

standardDeviation

Returns the standard deviation of an array of numbers.

Use Array.reduce() to calculate the mean, variance and the sum of the variance of the values, the variance of the values, then determine the standard deviation. You can omit the second argument to get the sample standard deviation or set it to true to get the population standard deviation.

const standardDeviation = (arr, usePopulation = false) => {
   const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length;
   return Math.sqrt(
@@ -853,7 +853,7 @@ console.log<
 

objectFromPairs

Creates an object from the given key-value pairs.

Use Array.reduce() to create and combine key-value pairs.

const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {});
 
objectFromPairs([['a', 1], ['b', 2]]); // {a: 1, b: 2}
 

objectToPairs

Creates an array of key-value pair arrays from an object.

Use Object.keys() and Array.map() to iterate over the object's keys and produce an array with key-value pairs.

const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
-
objectToPairs({ a: 1, b: 2 }); // [['a',1],['b',2]])
+
objectToPairs({ a: 1, b: 2 }); // [['a',1],['b',2]]
 

orderBy

Returns a sorted array of objects ordered by properties and orders.

Uses Array.sort(), Array.reduce() on the props array with a default value of 0, use array destructuring to swap the properties position depending on the order passed. If no orders array is passed it sort by 'asc' by default.

const orderBy = (arr, props, orders) =>
   [...arr].sort((a, b) =>
     props.reduce((acc, prop, i) => {
@@ -944,7 +944,7 @@ console.log<
   ('' + cc).slice(0, -num).replace(/./g, mask) + ('' + cc).slice(-num);
 
mask(1234567890); // '******7890'
 mask(1234567890, 3); // '*******890'
-mask(1234567890, -4, '$'); // '1234$$$$$$'
+mask(1234567890, -4, '$'); // '$$$$567890'
 

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 (
@@ -978,7 +978,7 @@ console.log<
 

sortCharactersInString

Alphabetically sorts the characters in a string.

Use the spread operator (...), Array.sort() and String.localeCompare() to sort the characters in str, recombine using String.join('').

const sortCharactersInString = str => [...str].sort((a, b) => a.localeCompare(b)).join('');
 
sortCharactersInString('cabbage'); // 'aabbceg'
 

splitLines

Splits a multiline string into an array of lines.

Use String.split() and a regular expression to match line breaks and create an array.

const splitLines = str => str.split(/\r?\n/);
-
splitLines('This\nis a\nmultiline\nstring.\n'); // ['This', 'is a', 'multiline', 'string' , '']
+
splitLines('This\nis a\nmultiline\nstring.\n'); // ['This', 'is a', 'multiline', 'string.' , '']
 

toCamelCase

Converts a string to camelcase.

Break the string into words and combine them capitalizing the first letter of each word, using a regexp.

const toCamelCase = str => {
   let s =
     str &&
@@ -1059,7 +1059,7 @@ console.log<
 

isNumber

Checks if the given argument is a number.

Use typeof to check if a value is classified as a number primitive.

const isNumber = val => typeof val === 'number';
 
isNumber('1'); // false
 isNumber(1); // true
-

isPrimitive

Returns a boolean determining if the supplied value is primitive or not.

Use Array.includes() on an array of type strings which are not primitive, supplying the type using typeof. Since typeof null evaluates to 'object', it needs to be directly compared.

const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === null;
+

isPrimitive

Returns a boolean determining if the passed value is primitive or not.

Use Array.includes() on an array of type strings which are not primitive, supplying the type using typeof. Since typeof null evaluates to 'object', it needs to be directly compared.

const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === null;
 
isPrimitive(null); // true
 isPrimitive(50); // true
 isPrimitive('Hello!'); // true
@@ -1143,9 +1143,9 @@ console.log<
   const n = Number(((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(precision));
   return (num < 0 ? '-' : '') + n + (addSpace ? ' ' : '') + UNITS[exponent];
 };
-
prettyBytes(1000); // 1 KB
-prettyBytes(-27145424323.5821, 5); // -27.145 GB
-prettyBytes(123456789, 3, false); // 123MB
+
prettyBytes(1000); // "1 KB"
+prettyBytes(-27145424323.5821, 5); // "-27.145 GB"
+prettyBytes(123456789, 3, false); // "123MB"
 

randomHexColorCode

Generates a random hexadecimal color code.

Use Math.random to generate a random 24-bit(6x4bits) hexadecimal number. Use bit shifting and then convert it to an hexadecimal String using toString(16).

const randomHexColorCode = () => {
   let n = ((Math.random() * 0xfffff) | 0).toString(16);
   return '#' + (n.length !== 6 ? ((Math.random() * 0xf) | 0).toString(16) + n : n);
@@ -1161,7 +1161,7 @@ console.log<
 };
 
timeTaken(() => Math.pow(2, 10)); // 1024, (logged): timeTaken: 0.02099609375ms
 

toDecimalMark

Use toLocaleString() to convert a float-point arithmetic to the Decimal mark form. It makes a comma separated string from a number.

const toDecimalMark = num => num.toLocaleString('en-US');
-
toDecimalMark(12305030388.9087); // "12,305,030,388.9087"
+
toDecimalMark(12305030388.9087); // "12,305,030,388.909"
 

toOrdinalSuffix

Adds an ordinal suffix to a number.

Use the modulo operator (%) to find values of single and tens digits. Find which ordinal pattern digits match. If digit is found in teens pattern, use teens ordinal.

const toOrdinalSuffix = num => {
   const int = parseInt(num),
     digits = [int % 10, int % 100],