@awesomeness-js/utils 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +23 -8
- package/package.json +1 -1
- package/src/convertBytes.js +8 -0
- package/src/each.js +17 -0
- package/src/eachAsync.js +21 -0
- package/src/getAllFiles.js +24 -0
- package/src/toPennies.js +19 -0
- package/test.js +4 -0
package/index.js
CHANGED
|
@@ -4,17 +4,32 @@
|
|
|
4
4
|
* Do not edit manually.
|
|
5
5
|
*/
|
|
6
6
|
import _build from './src/build.js';
|
|
7
|
+
import _convertBytes from './src/convertBytes.js';
|
|
8
|
+
import _each from './src/each.js';
|
|
9
|
+
import _eachAsync from './src/eachAsync.js';
|
|
10
|
+
import _getAllFiles from './src/getAllFiles.js';
|
|
11
|
+
import _toPennies from './src/toPennies.js';
|
|
7
12
|
|
|
8
13
|
export { _build as build };
|
|
14
|
+
export { _convertBytes as convertBytes };
|
|
15
|
+
export { _each as each };
|
|
16
|
+
export { _eachAsync as eachAsync };
|
|
17
|
+
export { _getAllFiles as getAllFiles };
|
|
18
|
+
export { _toPennies as toPennies };
|
|
9
19
|
|
|
10
20
|
export default {
|
|
11
|
-
/**
|
|
12
|
-
* Generates a output file that consolidates all src functions.
|
|
13
|
-
*
|
|
14
|
-
* @param {Object} [options] - The options for generating the output file.
|
|
15
|
-
* @param {string} [options.src='./src'] - The source directory.
|
|
16
|
-
* @param {array} [options.dest=['./', 'index.js']] - The destination file.
|
|
17
|
-
* @returns {bool} - Returns true if the output file is generated successfully.
|
|
21
|
+
/**
|
|
22
|
+
* Generates a output file that consolidates all src functions.
|
|
23
|
+
*
|
|
24
|
+
* @param {Object} [options] - The options for generating the output file.
|
|
25
|
+
* @param {string} [options.src='./src'] - The source directory.
|
|
26
|
+
* @param {array} [options.dest=['./', 'index.js']] - The destination file.
|
|
27
|
+
* @returns {bool} - Returns true if the output file is generated successfully.
|
|
18
28
|
*/
|
|
19
|
-
build: _build
|
|
29
|
+
build: _build,
|
|
30
|
+
convertBytes: _convertBytes,
|
|
31
|
+
each: _each,
|
|
32
|
+
eachAsync: _eachAsync,
|
|
33
|
+
getAllFiles: _getAllFiles,
|
|
34
|
+
toPennies: _toPennies
|
|
20
35
|
};
|
package/package.json
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export default function convertBytes(bytes, precision = 2) {
|
|
2
|
+
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
3
|
+
bytes = Math.max(bytes, 0);
|
|
4
|
+
const pow = Math.floor((bytes ? Math.log(bytes) : 0) / Math.log(1024));
|
|
5
|
+
const index = Math.min(pow, units.length - 1);
|
|
6
|
+
bytes /= 2 ** (10 * pow);
|
|
7
|
+
return `${bytes.toFixed(precision)} ${units[index]}`;
|
|
8
|
+
};
|
package/src/each.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export default function each(objectOrArray, callback) {
|
|
2
|
+
|
|
3
|
+
if(Array.isArray(objectOrArray)){
|
|
4
|
+
|
|
5
|
+
objectOrArray.every( (v, k) => {
|
|
6
|
+
if(callback( v, k ) === false){ return false; } else { return true; }
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
} else {
|
|
10
|
+
|
|
11
|
+
Object.entries(objectOrArray).every(entry => {
|
|
12
|
+
if(callback( entry[1], entry[0] ) === false){ return false; } else { return true; }
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
};
|
package/src/eachAsync.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export default async function eachAsync(objectOrArray, callback) {
|
|
2
|
+
if (Array.isArray(objectOrArray)) {
|
|
3
|
+
// Use a for...of loop for arrays
|
|
4
|
+
for (const [k, v] of objectOrArray.entries()) {
|
|
5
|
+
// Await the callback
|
|
6
|
+
if (await callback(v, k) === false) {
|
|
7
|
+
break;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
} else {
|
|
11
|
+
// Use a for...in loop for objects
|
|
12
|
+
for (const k in objectOrArray) {
|
|
13
|
+
if (objectOrArray.hasOwnProperty(k)) {
|
|
14
|
+
// Await the callback
|
|
15
|
+
if (await callback(objectOrArray[k], k) === false) {
|
|
16
|
+
break;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
function getAllFiles(dirPath = './', arrayOfFiles = []){
|
|
5
|
+
|
|
6
|
+
const items = fs.readdirSync(dirPath);
|
|
7
|
+
|
|
8
|
+
items.forEach((item) => {
|
|
9
|
+
const fullPath = path.resolve(dirPath, item);
|
|
10
|
+
|
|
11
|
+
if (fs.statSync(fullPath).isDirectory()) {
|
|
12
|
+
// Recurse into the directory
|
|
13
|
+
getAllFiles(fullPath, arrayOfFiles);
|
|
14
|
+
} else {
|
|
15
|
+
// Add only files
|
|
16
|
+
arrayOfFiles.push(fullPath);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return arrayOfFiles;
|
|
21
|
+
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default getAllFiles;
|
package/src/toPennies.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default function toPennies(uglyMoney){
|
|
2
|
+
|
|
3
|
+
if(!uglyMoney){ return 0; }
|
|
4
|
+
if(uglyMoney.length == 0){ return 0; }
|
|
5
|
+
|
|
6
|
+
// trim all whitespace from string
|
|
7
|
+
uglyMoney = uglyMoney.replace(/\s/g,'');
|
|
8
|
+
|
|
9
|
+
let hasDecimal = uglyMoney.includes(".");
|
|
10
|
+
let cleanMoney_v1 = uglyMoney.replace(/\D/g,'');
|
|
11
|
+
|
|
12
|
+
if(cleanMoney_v1.length == 0){ return 0; }
|
|
13
|
+
|
|
14
|
+
let cleanMoney = cleanMoney_v1*1;
|
|
15
|
+
|
|
16
|
+
if(!hasDecimal){ cleanMoney = cleanMoney * 100; }
|
|
17
|
+
|
|
18
|
+
return cleanMoney;
|
|
19
|
+
};
|
package/test.js
ADDED