This repository has been archived by the owner on Jul 31, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.js
83 lines (77 loc) · 2.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Uppercase the first character of a string.
*
* @param {string} str String to uppercase the first letter of
* @returns {string} Original string with the first letter in uppercase
*/
export const capitalize = (str) => str[0].toUpperCase() + str.slice(1);
/**
* Formats a number as currency using the user's locale. 1234.56 => '$1,234.56'
*
* @param {number} num The number to be formatted
* @param {string} currency The type of currency, such as 'USD' or 'JPY'
* @param {string} locale The locale for formatting the number, such as 'en-US'. Defaults to navigator.language
* @returns {string} The number as a string formatted in the locale of the user
*/
export function currency(num, currency, locale = navigator.language) {
// Alternative: (73.57).toLocaleString('de-DE',{style:'currency',currency:'EUR'});
return new Intl.NumberFormat(locale, {
style: 'currency',
currency,
}).format(num);
}
/**
* Formats a number to a culture code locale. 1234 => '1,234'
*
* @param {number} num The number to be formatted
* @param {string} locale Culture-code for formatting the number, such as 'en-US'. Defaults to navigator.language
* @returns {string} A formmated number based on a culture code
*/
export function number(num, locale = navigator.language) {
return new Intl.NumberFormat(locale).format(num);
}
/**
* Takes in a value and a fallback. Return fallback if value is falsy.
*
* @param {string} str Desired text to display
* @param {string} placeholder Fallback text
* @returns {string} Shows desired text unless falsy, then shows fallback
*/
export const placeholder = (str, placeholder) => str || placeholder;
/**
* Returns the plural or singular form of a word based on a amount
*
* @param {string} singular The singular form of a noun, like "entity"
* @param {string} plural The plural form of a noun, like "entities"
* @param {number} amount The amount of the item the noun represents
* @returns {string} The correct plurality
*/
export function plural(singular, plural, amount) {
return amount !== 1 ? plural : singular;
}
/**
* Replaces all characters of a string that surpass a length with a new string.
*
* @param {string} str String of text to be truncated
* @param {number} length Amount of characters to allow before truncating. Defaults to 100
* @param {string} append Text to append to the end of a truncated string. Defaults to '...'
* @returns {string} The original string, or a truncated version if it exceed the allowed limit.
*/
export function truncate(str, length = 100, append = '...') {
// TODO: dont split last word
if (str.length > length) {
return str.substring(0, length - append.length) + append;
} else {
return str;
}
}
/**
* TODO:
* (https://www.npmjs.com/package/vue2-filters)
* truncate
* scientific notation
* filterBy
* find
* sortBy
* mask?
*/