cleaner-node
Advanced tools
Comparing version 0.3.10 to 0.4.0
@@ -26,3 +26,5 @@ module.exports = { | ||
"no-console": "off", | ||
"no-continue": "off", | ||
"no-multi-spaces": "off", | ||
"no-nested-ternary": "off", | ||
"no-restricted-globals": "off", | ||
@@ -29,0 +31,0 @@ "no-shadow": "off", |
{ | ||
"name": "cleaner-node", | ||
"version": "0.3.10", | ||
"version": "0.4.0", | ||
"description": "Helpful utilities and scripts to make Node projects more legible and easier for the next developer to take over.", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "eslint src/**/*.js", | ||
"lint:fix": "eslint --fix src/**/*.js" | ||
"test": "eslint src/**/*.js", | ||
"test:fix": "eslint --fix src/**/*.js" | ||
}, | ||
@@ -48,2 +48,3 @@ "repository": { | ||
"eslint": "^5.9.0", | ||
"eslint-config-airbnb": "^17.1.0", | ||
"eslint-config-standard": "^12.0.0", | ||
@@ -50,0 +51,0 @@ "eslint-plugin-import": "^2.14.0", |
@@ -10,2 +10,3 @@ # cleaner-node | ||
| 0.3.6 | 2019/02/18 | Add `strings.getByteCount` to determine true size of string rather than simply using `.length`. | | ||
| 0.3.7 | 2019/02/26 | Add `files` utility. | | ||
| 0.3.7 | 2019/02/26 | Add `files` utility. | | ||
| 0.4.0 | 2019/02/27 | Add `.fileContents`, `.lock`, `.unlock`, `.isLocked`, `.cleanLock` functions to `files`. | |
const returnValue = (valueOrValues, values) => { | ||
if (typeof valueOrValues === 'object' && valueOrValues instanceof Array) { | ||
return values; | ||
} else { | ||
return (values.length > 0) ? values[0] : undefined; | ||
} | ||
return (values.length > 0) ? values[0] : undefined; | ||
}; | ||
@@ -11,2 +10,2 @@ | ||
returnValue | ||
}; | ||
}; |
@@ -18,3 +18,3 @@ const email = require('email-addresses'); | ||
const address = email.parseOneAddress(value); | ||
return (address.local + '@' + address.domain); | ||
return `${address.local}'@'${address.domain}`; | ||
}; | ||
@@ -21,0 +21,0 @@ |
@@ -17,3 +17,3 @@ const constants = require('./constants'); | ||
var message = (typeof errorOrMessage === 'string') | ||
const message = (typeof errorOrMessage === 'string') | ||
? errorOrMessage | ||
@@ -20,0 +20,0 @@ : errorOrMessage.message; |
114
src/files.js
const fs = require('fs'); | ||
const path = require('path'); | ||
const { isValid: isValidString } = require('./strings'); | ||
const LOCK_SUFFIX = '.lock'; | ||
// ----- GENERAL | ||
const getStats = itemPath => { | ||
@@ -12,3 +17,15 @@ try { | ||
} | ||
const toPath = value => { | ||
if (!isValidString(value)) { return undefined; } | ||
try { | ||
const result = path.parse(value); | ||
return (result && | ||
isValidString(result.root) && | ||
(isValidString(result.dir) || isValidString(result.base))) ? result : undefined; | ||
} catch (ex) { | ||
return undefined; | ||
} | ||
} | ||
const isFile = itemPath => { | ||
if (!isValidString(itemPath)) { return false; } | ||
const stats = getStats(itemPath); | ||
@@ -18,6 +35,11 @@ return stats && stats.isFile(); | ||
const isFolder = itemPath => { | ||
if (!isValidString(itemPath)) { return false; } | ||
const stats = getStats(itemPath); | ||
return stats && stats.isDirectory(); | ||
} | ||
// ----- CONTENTS | ||
const folderContents = folderPath => { | ||
if (!isValidString(folderPath)) { return undefined; } | ||
try { | ||
@@ -30,3 +52,34 @@ const names = fs.readdirSync(folderPath); | ||
} | ||
const fileContents = (filePath, options) => { | ||
if (!isValidString(filePath)) { return undefined; } | ||
try { | ||
const contents = fs.readFileSync(filePath, options); | ||
return contents; | ||
} catch (ex) { | ||
return undefined; | ||
} | ||
} | ||
const writeFile = (filePath, contents = '', overwrite = false) => { | ||
if (isFile(filePath) && !overwrite) { return false; } | ||
try { | ||
fs.writeFileSync(filePath, String(contents)); | ||
} catch (ex) { | ||
return false; | ||
} | ||
return isFile(filePath); | ||
} | ||
const deleteFile = filePath => { | ||
if (!isFile(filePath)) { return true; } | ||
try { | ||
fs.unlinkSync(filePath); | ||
} catch (ex) { | ||
return !isFile(filePath); | ||
} | ||
return !isFile(filePath); | ||
} | ||
// ----- WALKING | ||
const walkFolder = (folderPath, results) => { | ||
if (!isValidString(folderPath)) { return; } | ||
const paths = folderContents(folderPath) || []; | ||
@@ -41,3 +94,2 @@ paths.forEach(p => { | ||
}); | ||
return; | ||
}; | ||
@@ -55,8 +107,64 @@ const walk = (folderPath) => { | ||
/** | ||
┌─────────────────────┬────────────┐ | ||
│ dir │ base │ | ||
├──────┬ ├──────┬─────┤ | ||
│ root │ │ name │ ext │ | ||
" C:\ path\dir \ file .txt " | ||
└──────┴──────────────┴──────┴─────┘ | ||
*/ | ||
// --- LOCKING --- | ||
const isLocked = filePath => { | ||
if (!isValidString(filePath)) { return false; } | ||
return (isFile(filePath) && isFile(filePath + LOCK_SUFFIX)); | ||
} | ||
const lock = filePath => { | ||
if (!isValidString(filePath)) { return false; } | ||
if (!isFile(filePath)) { return false; } | ||
if (isFile(filePath + LOCK_SUFFIX)) { return true; } | ||
writeFile(filePath + LOCK_SUFFIX, (new Date()).toISOString()); | ||
return isLocked(filePath); | ||
} | ||
const unlock = filePath => { | ||
if (!isValidString(filePath)) { return false; } | ||
if (!isFile(filePath + LOCK_SUFFIX)) { return true; } | ||
deleteFile(filePath + LOCK_SUFFIX); | ||
return !isLocked(filePath); | ||
} | ||
const cleanLock = filePath => { | ||
if (!isValidString(filePath)) { return; } | ||
const info = toPath(filePath); | ||
if (!info) { return; } | ||
if (isFile(filePath + LOCK_SUFFIX) && !isFile(filePath)) { | ||
deleteFile(filePath + LOCK_SUFFIX); | ||
} | ||
} | ||
module.exports = { | ||
getStats, | ||
toPath, | ||
isFile, | ||
isFolder, | ||
fileContents, | ||
folderContents, | ||
walk | ||
} | ||
writeFile, | ||
deleteFile, | ||
walk, | ||
isLocked, | ||
lock, | ||
unlock, | ||
cleanLock | ||
}; |
@@ -83,2 +83,2 @@ const jwt = require('jsonwebtoken'); | ||
isValidPayload | ||
} | ||
}; |
const constants = require('./constants'); | ||
const DIGITS = constants.strings.DIGITS; | ||
const { DIGITS } = constants.strings; | ||
@@ -15,7 +15,7 @@ const sortAscending = (a, b) => { | ||
return value.toString().split('').filter(x => DIGITS.indexOf(x) >= 0); | ||
} else if (typeof value === 'string') { | ||
} | ||
if (typeof value === 'string') { | ||
return value.split('').filter(x => DIGITS.indexOf(x) >= 0); | ||
} else { | ||
return null; | ||
} | ||
return null; | ||
} | ||
@@ -22,0 +22,0 @@ |
@@ -16,3 +16,3 @@ const { isValid: isValidString } = require('./strings'); | ||
const result = []; | ||
items.forEach(function (item) { | ||
items.forEach(item => { | ||
const id = getId(item); | ||
@@ -34,3 +34,3 @@ if (typeof id !== 'undefined') { | ||
const result = []; | ||
items.forEach(function (item) { | ||
items.forEach(item => { | ||
const id = getUid(item); | ||
@@ -101,3 +101,3 @@ if (typeof id !== 'undefined') { | ||
function getValue (item, keyOrPath) { | ||
function getValue(item, keyOrPath) { | ||
const keys = keyOrPath.split('.'); | ||
@@ -115,3 +115,3 @@ const used = []; | ||
} | ||
function getValues (items, keyOrPath, allowDuplicates) { | ||
function getValues(items, keyOrPath, allowDuplicates) { | ||
const result = []; | ||
@@ -129,3 +129,3 @@ [].concat(items).filter(isValid).forEach(item => { | ||
function setValue (obj = {}, path, value) { | ||
function setValue(obj = {}, path, value) { | ||
const keys = path.split('.'); | ||
@@ -132,0 +132,0 @@ const used = []; |
const strings = require('./strings'); | ||
const isValid = (value) => { | ||
const v = strings.isValid(value) ? value : ((typeof value === 'number') ? String(value) : undefined); | ||
const v = strings.isValid(value) | ||
? value | ||
: ((typeof value === 'number') | ||
? String(value) | ||
: undefined); | ||
if (!strings.isValid(v) || [1, 5, 6, 10, 11].indexOf(v.length) < 0) { return false; } | ||
@@ -12,2 +16,2 @@ if (v.length === 11 && v.split('')[0] !== '1') { return false; } | ||
isValid | ||
} | ||
}; |
@@ -27,3 +27,3 @@ const cc = require('camelcase'); | ||
if (!isValid(value)) { return false; } | ||
const length = (trim ? value.trim() : value).length; | ||
const { length } = (trim ? value.trim() : value); | ||
if (min > 0 && length < min) { return false; } | ||
@@ -121,3 +121,5 @@ if (max > 0 && length > max) { return false; } | ||
for (let i = 0; i < values.length; i += 1) { | ||
if (typeof values[i] !== 'string') { continue; } | ||
if (typeof values[i] !== 'string') { | ||
continue; | ||
} | ||
values[i] = values[i].trim(); | ||
@@ -166,5 +168,4 @@ if (values[i].length === 0 && toUndefined) { | ||
return (value.indexOf(prefix) === 0); | ||
} else { | ||
return (value.toLowerCase().indexOf(prefix.toLowerCase()) === 0); | ||
} | ||
return (value.toLowerCase().indexOf(prefix.toLowerCase()) === 0); | ||
} | ||
@@ -188,5 +189,4 @@ const removePrefix = (value, prefix, isCaseSensitive = true) => { | ||
return value.endsWith(prefix); | ||
} else { | ||
return (value.toLowerCase().endsWith(prefix.toLowerCase())); | ||
} | ||
return (value.toLowerCase().endsWith(prefix.toLowerCase())); | ||
} | ||
@@ -193,0 +193,0 @@ const removeSuffix = (value, suffix, isCaseSensitive = true) => { |
@@ -41,2 +41,2 @@ const strings = require('./strings'); | ||
└────────────────────────────────────────────────────────────────────────────────────────────────┘ | ||
*/ | ||
*/ |
@@ -15,3 +15,3 @@ const uuidV1 = require('uuid/v1'); | ||
const isGuidFormat = value => { | ||
if (!strings.isValidChars(value, ALPHANUMERIC + '-', false)) { | ||
if (!strings.isValidChars(value, `${ALPHANUMERIC}-`, false)) { | ||
return false; | ||
@@ -30,3 +30,3 @@ } | ||
const isUidFormat = value => { | ||
return strings.isValidChars(value, ALPHANUMERIC, false) && value.length == EMPTY_UID.length; | ||
return strings.isValidChars(value, ALPHANUMERIC, false) && value.length === EMPTY_UID.length; | ||
} | ||
@@ -113,3 +113,3 @@ | ||
const cache = []; | ||
[].concat(values).filter(isValid).forEach(function (x) { | ||
[].concat(values).filter(isValid).forEach(x => { | ||
if (x && cache.indexOf(toUidFormat(x)) < 0) { | ||
@@ -116,0 +116,0 @@ cache.push(toUidFormat(x)); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
50826
1192
11
7