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)); + }
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
narguments, ignoring any additional arguments.Call the provided function,
fn, with up tonarguments, usingArray.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); @@ -1847,6 +1847,13 @@ Logs: { return r; };timeTaken(() => Math.pow(2, 10)); // 1024, (logged): timeTaken: 0.02099609375ms +toCurrency
Take a number and return specified currency formatting.
Use
Intl.NumberFormatto enable country / currency sensitive formatting.const toCurrency = (n, curr, LanguageFormat = undefined) => + Intl.NumberFormat(LanguageFormat, { style: 'currency', currency: curr }).format(n); +toCurrency(123456.789, 'EUR'); // €123,456.79 | currency: Euro | currencyLangFormat: Local +toCurrency(123456.789, 'USD', 'en-us'); // €123,456.79 | currency: US Dollar | currencyLangFormat: English (United States) +toCurrency(123456.789, 'USD', 'fa'); // ۱۲۳٬۴۵۶٫۷۹ $ | currency: US Dollar | currencyLangFormat: Farsi +toCurrency(322342436423.2435, 'JPY'); // ¥322,342,436,423 | currency: Japanese Yen | currencyLangFormat: Local +toCurrency(322342436423.2435, 'JPY', 'fi'); // 322 342 436 423 ¥ | currency: Japanese Yen | currencyLangFormat: FinnishtoDecimalMark
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.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 => { diff --git a/snippets/toCurrency.md b/snippets/toCurrency.md index bc690aa68..bfb5561da 100644 --- a/snippets/toCurrency.md +++ b/snippets/toCurrency.md @@ -5,13 +5,14 @@ Take a number and return specified currency formatting. Use `Intl.NumberFormat` to enable country / currency sensitive formatting. ```js -const toCurrency = (n, curr, LanguageFormat = undefined) => Intl.NumberFormat(LanguageFormat, { style: 'currency', currency: curr }).format(n); +const toCurrency = (n, curr, LanguageFormat = undefined) => + Intl.NumberFormat(LanguageFormat, { style: 'currency', currency: curr }).format(n); ``` ```js -toCurrency(123456.789, 'EUR') // €123,456.79 | currency: Euro | currencyLangFormat: Local -toCurrency(123456.789, 'USD', 'en-us') // €123,456.79 | currency: US Dollar | currencyLangFormat: English (United States) -toCurrency(123456.789, 'USD', 'fa') // ۱۲۳٬۴۵۶٫۷۹ $ | currency: US Dollar | currencyLangFormat: Farsi -toCurrency(322342436423.2435, 'JPY') // ¥322,342,436,423 | currency: Japanese Yen | currencyLangFormat: Local -toCurrency(322342436423.2435, 'JPY', 'fi') // 322 342 436 423 ¥ | currency: Japanese Yen | currencyLangFormat: Finnish +toCurrency(123456.789, 'EUR'); // €123,456.79 | currency: Euro | currencyLangFormat: Local +toCurrency(123456.789, 'USD', 'en-us'); // €123,456.79 | currency: US Dollar | currencyLangFormat: English (United States) +toCurrency(123456.789, 'USD', 'fa'); // ۱۲۳٬۴۵۶٫۷۹ $ | currency: US Dollar | currencyLangFormat: Farsi +toCurrency(322342436423.2435, 'JPY'); // ¥322,342,436,423 | currency: Japanese Yen | currencyLangFormat: Local +toCurrency(322342436423.2435, 'JPY', 'fi'); // 322 342 436 423 ¥ | currency: Japanese Yen | currencyLangFormat: Finnish ```