Skip to content

Commit 82d4512

Browse files
boilerplate 6 built-in library function add
1 parent 90c09c7 commit 82d4512

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const arrayToCommaSeparatedText = (array) => (array ?
2+
array
3+
.map((item) => item)
4+
.join(', ')
5+
.toString() :
6+
'None');
7+
8+
export default arrayToCommaSeparatedText;

src/lib/downloadString.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const downloadString = (data, filename, ext) => {
2+
const dataStr = `data:text/json;charset=utf-8,${encodeURIComponent(data)}`;
3+
const downloadAnchorNode = document.createElement('a');
4+
5+
downloadAnchorNode.setAttribute('href', dataStr);
6+
downloadAnchorNode.setAttribute('download', `${filename}.${ext}`);
7+
8+
document.body.appendChild(downloadAnchorNode); // required for firefox
9+
10+
downloadAnchorNode.click();
11+
downloadAnchorNode.remove();
12+
};
13+
14+
export default downloadString;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function convertToInternationalCurrencySystem(labelValue) {
2+
// Nine Zeroes for Billions
3+
return Math.abs(Number(labelValue)) >= 1.0e9 ?
4+
`${(Math.abs(Number(labelValue)) / 1.0e9).toFixed(0)} B` : // Six Zeroes for Millions
5+
Math.abs(Number(labelValue)) >= 1.0e6 ?
6+
`${(Math.abs(Number(labelValue)) / 1.0e6).toFixed(0)} M` : // Three Zeroes for Thousands
7+
Math.abs(Number(labelValue)) >= 1.0e3 ?
8+
`${(Math.abs(Number(labelValue)) / 1.0e3).toFixed(0)} K` :
9+
Math.abs(Number(labelValue));
10+
}
11+
12+
export default convertToInternationalCurrencySystem;

src/lib/isEmptyObject.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint-disable no-restricted-syntax */
2+
function isEmptyObject(obj) {
3+
let name;
4+
for (name in obj) {
5+
if (obj.hasOwnProperty(name)) {
6+
return false;
7+
}
8+
}
9+
return true;
10+
}
11+
12+
export default isEmptyObject;

src/lib/truncateString.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function truncateString(str, max, sep) {
2+
// Default to 10 characters
3+
max = max || 10;
4+
5+
const len = str.length;
6+
if (len > max) {
7+
// Default to ellipsis
8+
sep = sep || '...';
9+
10+
const sepLen = sep.length;
11+
12+
// If separator is larger than character limit,
13+
// well then we don't want to just show the separator,
14+
// so just show right hand side of the string.
15+
if (sepLen > max) {
16+
return str.substr(len - max);
17+
}
18+
19+
// Half the difference between max and string length.
20+
// Multiply negative because small minus big.
21+
// Must account for length of separator too.
22+
const n = -0.5 * (max - len - sepLen);
23+
24+
// This gives us the centerline.
25+
const center = len / 2;
26+
27+
const front = str.substr(0, center - n);
28+
const back = str.substr(len - center + n); // without second arg, will automatically go to end of line.
29+
30+
return front + sep + back;
31+
}
32+
33+
return str;
34+
}
35+
36+
export default truncateString;

src/lib/waitSomeMoment.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function wait(ms) {
2+
const start = new Date().getTime();
3+
let end = start;
4+
5+
while (end < start + ms) {
6+
end = new Date().getTime();
7+
}
8+
}
9+
10+
export default wait;

0 commit comments

Comments
 (0)