pixl-tools
Advanced tools
Comparing version 1.0.19 to 1.0.20
{ | ||
"name": "pixl-tools", | ||
"version": "1.0.19", | ||
"version": "1.0.20", | ||
"description": "A set of miscellaneous utility functions for Node.js.", | ||
@@ -22,3 +22,3 @@ "author": "Joseph Huckaby <jhuckaby@gmail.com>", | ||
"dependencies": { | ||
"errno": "^0.1.7", | ||
"errno": "0.1.7", | ||
"async": "2.6.1", | ||
@@ -25,0 +25,0 @@ "mkdirp": "0.5.1", |
@@ -79,2 +79,3 @@ # Overview | ||
| [normalizeTime()](#normalizetime) | Normalize (floor) Epoch seconds into nearest minute, hour, day, etc. | | ||
| [formatDate()](#formatdate) | Format date/time in local timezone using a template string. | | ||
| [getTextFromBytes()](#gettextfrombytes) | Convert a byte count into a human readable string, e.g. `5 MB`. | | ||
@@ -555,20 +556,26 @@ | [getBytesFromText()](#getbytesfromtext) | Convert a human-readable size (e.g. `5 MB`) into a raw byte count. | | ||
| `year` | 2015 | Full year as integer. | | ||
| `yy` | "15" | 2-digit year as string, with padded zeros if needed. | | ||
| `yyyy` | "2015" | 4-digit year as string. | | ||
| `mon` | 3 | Month of year as integer (1 - 12). | | ||
| `mm` | "03" | 2-digit month as string with padded zeros if needed. | | ||
| `mmm` | "Mar" | Month name abbreviated to first three letters. | | ||
| `mmmm` | "March" | Full month name. | | ||
| `mday` | 6 | Day of month as integer (1 - 31). | | ||
| `wday` | 4 | Day of week as integer (0 - 6). | | ||
| `dd` | "06" | 2-digit day as string with padded zeros if needed. | | ||
| `wday` | 4 | Day of week as integer (0 - 6), starting with Sunday. | | ||
| `ddd` | "Thu" | Weekday name abbreviated to first three letters. | | ||
| `dddd` | "Thursday" | Full weekday name. | | ||
| `hour` | 9 | Hour of day as integer (0 - 23). | | ||
| `hour12` | 9 | Hour expressed in 12-hour time (i.e. 3 PM = 3). | | ||
| `hh` | "09" | 2-digit hour as string with padded zeros if needed. | | ||
| `min` | 2 | Minute of hour as integer (0 - 59). | | ||
| `mi` | "02" | 2-digit minute as string with padded zeros if needed. | | ||
| `sec` | 10 | Second of minute as integer (0 - 59). | | ||
| `ss` | "10" | 2-digit second as string with padded zeros if needed. | | ||
| `msec` | 999 | Millisecond of second as integer (0 - 999). | | ||
| `yyyy` | "2015" | 4-digit year as string. | | ||
| `mm` | "03" | 2-digit month as string with padded zeros if needed. | | ||
| `dd` | "06" | 2-digit day as string with padded zeros if needed. | | ||
| `hh` | "09" | 2-digit hour as string with padded zeros if needed. | | ||
| `mi` | "02" | 2-digit minute as string with padded zeros if needed. | | ||
| `ss` | "10" | 2-digit second as string with padded zeros if needed. | | ||
| `hour12` | 9 | Hour expressed in 12-hour time (i.e. 1 PM = 1.) | | ||
| `ampm` | "am" | String representing ante meridiem (`am`) or post meridiem (`pm`). | | ||
| `AMPM` | "AM" | Upper-case version of `ampm`. | | ||
| `yyyy_mm_dd` | "2015/03/06" | Formatted string representing date in `YYYY/MM/DD` format. | | ||
| `hh_mi_ss` | "09:02:10" | Formatted string representing local time in `HH:MI:SS` format. | | ||
| `epoch` | 1425661330 | Epoch seconds used to generate all the date args. | | ||
| `epoch` | 1425661330 | Epoch seconds used to generate all the date properties. | | ||
| `offset` | -28800 | Local offset from GMT/UTC in seconds. | | ||
@@ -619,2 +626,23 @@ | `tz` | "GMT-8" | Formatted GMT hour offset string. | | ||
## formatDate | ||
``` | ||
STRING formatDate( MIXED, STRING ) | ||
``` | ||
This function parses any date string, Epoch timestamp or Date object, and produces a formatted date/time string according to a custom template, and in the local timezone. The template is populated using [sub()](#sub) (i.e. square bracket syntax) and can use any of the date/time properties returned by [getDateArgs()](#getdateargs). Examples: | ||
```js | ||
var now = new Date(); | ||
var str = Tools.formatDate( now, "[yyyy]/[mm]/[dd]" ); | ||
// 2019/03/22 | ||
var str = Tools.formatDate( now, "[dddd], [mmmm] [mday], [yyyy]" ); | ||
// Friday, March 22, 2019 | ||
var str = Tools.formatDate( now, "[hour12]:[mi] [ampm]" ); | ||
// 10:43 am | ||
``` | ||
## getTextFromBytes | ||
@@ -621,0 +649,0 @@ |
61
tools.js
@@ -12,2 +12,14 @@ // Misc Tools for Node.js | ||
var MONTH_NAMES = [ | ||
'January', 'February', 'March', 'April', 'May', 'June', | ||
'July', 'August', 'September', 'October', 'November', 'December' ]; | ||
var SHORT_MONTH_NAMES = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', | ||
'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec' ]; | ||
var DAY_NAMES = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', | ||
'Thursday', 'Friday', 'Saturday']; | ||
var SHORT_DAY_NAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; | ||
module.exports = { | ||
@@ -395,2 +407,8 @@ | ||
formatDate: function(thingy, template) { | ||
// format date using get_date_args | ||
// e.g. '[yyyy]/[mm]/[dd]' or '[dddd], [mmmm] [mday], [yyyy]' or '[hour12]:[mi] [ampm]' | ||
return this.sub( template, this.getDateArgs(thingy) ); | ||
}, | ||
getDateArgs: function(thingy) { | ||
@@ -415,2 +433,4 @@ // return hash containing year, mon, mday, hour, min, sec | ||
args.yyyy = '' + args.year; | ||
args.yy = args.year % 100; | ||
if (args.yy < 10) args.yy = "0" + args.yy; else args.yy = '' + args.yy; | ||
if (args.mon < 10) args.mm = "0" + args.mon; else args.mm = '' + args.mon; | ||
@@ -433,2 +453,3 @@ if (args.mday < 10) args.dd = "0" + args.mday; else args.dd = '' + args.mday; | ||
args.AMPM = args.ampm.toUpperCase(); | ||
args.yyyy_mm_dd = args.yyyy + '/' + args.mm + '/' + args.dd; | ||
@@ -438,2 +459,8 @@ args.hh_mi_ss = args.hh + ':' + args.mi + ':' + args.ss; | ||
// add formatted month and weekdays | ||
args.mmm = SHORT_MONTH_NAMES[ args.mon - 1 ]; | ||
args.mmmm = MONTH_NAMES[ args.mon - 1]; | ||
args.ddd = SHORT_DAY_NAMES[ args.wday ]; | ||
args.dddd = DAY_NAMES[ args.wday ]; | ||
return args; | ||
@@ -795,22 +822,20 @@ }, | ||
var lines = null; | ||
try { lines = fs.readFileSync('/etc/passwd', 'utf8').trim().split(/\n/); } | ||
var cols = null; | ||
//try { lines = fs.readFileSync('/etc/passwd', 'utf8').trim().split(/\n/); } | ||
var opts = { timeout: 1000, encoding: 'utf8', stdio: 'pipe' }; | ||
try { cols = cp.execSync('/usr/bin/getent passwd | grep ' + username, opts).trim().split(':'); } | ||
catch (err) { return null; } | ||
for (var idx = 0, len = lines.length; idx < len; idx++) { | ||
var cols = lines[idx].trim().split(':'); | ||
if ((username == cols[0]) || (username == Number(cols[2]))) { | ||
user = { | ||
username: cols[0], | ||
password: cols[1], | ||
uid: Number(cols[2]), | ||
gid: Number(cols[3]), | ||
name: cols[4] && cols[4].split(',')[0], | ||
dir: cols[5], | ||
shell: cols[6] | ||
}; | ||
idx = len; | ||
} // found user | ||
} // foreach line | ||
if (!user) { | ||
if ((username == cols[0]) || (username == Number(cols[2]))) { | ||
user = { | ||
username: cols[0], | ||
password: cols[1], | ||
uid: Number(cols[2]), | ||
gid: Number(cols[3]), | ||
name: cols[4] && cols[4].split(',')[0], | ||
dir: cols[5], | ||
shell: cols[6] | ||
}; | ||
} | ||
else { | ||
// user not found | ||
@@ -817,0 +842,0 @@ return null; |
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
61089
752
996
+ Addederrno@0.1.7(transitive)
- Removederrno@0.1.8(transitive)
Updatederrno@0.1.7