Utility functions for handling data types, errors, and unknown JavaScript values.
- Save time and code by using well tested, commonly sought functions
- Immediately find printout statements by having file and function names available
- Examine exact type of values with hidden properties and full prototype chains
- Separate logging by multiple instances by preceding with process id or host name
- Enhanced app portability across Linux, Mac and Windows
© 2011, 2013 Harald Rudell wrote haraldutil for node in October, 2011
- Conversion of any value to printable string representation
- Object manipulation methods
- Portable path, number and browser-launch functions
- Error and stack trace parsers and string conversions
- JavaScript time value, Unix timestamp and timezone functions
Console.log equivalent with added leading code location ```js var p = require('haraldutil').p
p('Printouts start with code location: file:line:function') p('In an anonymous function, the function name is omitted') someFunction()
function someFunction() { p('Value examples:', undefined, '1', new function Class() {this.a = 1}, JSON.stringify, new Error('a')) }
examples:15 Printouts start with code location: file:line:function examples:16 In an anonymous function like here, the function name is omitted examples:20:someFunction Leading string does format [object Object] NaN {"a":1} examples:21:someFunction undefined '1' { a: 1 } [Function: stringify] [Error: a]
</p>
<ul>
<li>return value: the string</li>
<li>ps is like p, but omits the console.log</li>
</ul>
<p>
```js
console.log('ps is p without logging, it can output location:', ps())
ps is p without logging, it can output location: examples:28
someFunction(undefined, '1', new function Class() {this.a = 1}, JSON.stringify, new Error('a'))
function someFunction() { pargs(arguments) }
examples:38:someFunction undefined, '1', object:Class {a: 1}, function stringify(), Error: a
<h2>pPrepend(str)</h2>
pPrepend adds a lead-in to functions p, ps, pargs, q, qs, pp and pps
```js
var haraldutil = require('haraldutil')
var p = haraldutil.p
var pPrepend = haraldutil.pPrepend
pPrepend(process.pid)
p('Launching of new process')
pPrepend(require('os').hostname() + ':' + process.pid)
p('Launching on new host')
19854:examples:51 'Launching of new process'
somehostname:19854:examples:54 'Launching on new host'
- pPrepend(): removes the current prepend value
- pPrepend(null): gets the current prepend value
someFunction()
function someFunction() { q('q and qs are like p and ps but does haraldutil.inspect rather then util.format') q(undefined, '1', new function Class() {this.a = 1}, JSON.stringify, new Error('a')) }
examples:61:someFunction 'q and qs are like p and ps but does haraldutil.inspect rather then util.format' examples:62:someFunction undefined '1' object:Class {a: 1} function stringify() Error: a
<h2>pp(...), pps(...)</h2>
Printout with code location and exhaustive inspect of values.
```js
var pp = require('haraldutil').pp
someFunction()
function someFunction() {
pp(console)
}
examples:17:someFunction {
info: function (),
log: recursive-object#2,
error: function (),
time: function (label),
warn: recursive-object#3,
dir: function (object),
trace: function (label),
assert: function (expression),
timeEnd: function (label)
}
- return value: the string
- pps is like pp, but omits the console.log
var haraldutil = require('haraldutil')
console.log(haraldutil.inspectDeep(console))
{
info:function (),
log:recursive-object#2,
error:function (),
time:function (label),
warn:recursive-object#3,
dir:function (object),
trace:function (label),
assert:function (expression),
timeEnd:function (label)
}
var haraldutil = require('haraldutil')
var a = 'abcdefghijklm'.split('')
console.log(haraldutil.inspect(a))
13:['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ..., 'm']
By default, the following steps shorten the printout
- Strings are shortened at 80 characters
- Non-enumerable properties or prototype chains are not printed
- Array-like properties are abbrevieted at 10 elements
- Max 2 levels of object properties are printed
optsArg
- maxString: optional number, default 80
- maxProperties: optional number, default 10
- maxLevels: optional number, default 2
- nonEnum: optional boolean, default false
- noArrayLength: optional boolean, default true: do not print array length
Clone an object and its enumerable properties
- o: value of array, object and primitive properties
- same name properties from later objects overwrite
- return value: Object object with only enumerable properties
- undefined: path1 does not exist
- 1: path1 is a directory
- true: path1 is a file
- .func: optional string: Object.function expression in the code
- .as: optional string: function name if different from property name
- .folder: optional string: if a folder other than current directory, then the absolute path to folder where source file is located, '/home/user'
- .file: optional string: source file name, 'script.js'
- .line: optional number: 10
- .column optional number: 5
- .source: optional string: text that may appear instead of file and folder, eg. 'unknown source'
- .text: string: this frame as text. contains no newlines and has the leading at removed
- empty string on troubles
- str: string: format: [+-]0-9..[.0-9..]
- allowFloat: optional boolean: allow decimal part, default no
- leading and trailing whitespace and line terminators are allowed
- unparseable numbers return NaN
- date: optional Date, default now
- timestamp: number: Unix epoch
- return value: Date object
- timestamp: optional number: unix timestamp, default: now
- timezoneMinutesOffUtc: optional number: minutes off utc, negative west of London, -240 for NY
- modifier: optional number: 1: skip date part, 2: skip date and timezone
- date a Date object, default now
- offset number, minutes from utc, default -420 (Pacific standard time)
- hour, minute: number: base time 0-23, 0-59
- tzOffset: offset from base location in minutes for result
- if base is in utc timezone and tzOffset is -240, result will be in eastern daylight time
- If any argument is not a non-empty string, exception is thrown
- For keys to match, every string argument at their creation must have been exactly the same
- num: timevalue, unit: ms, positive value
Array, Date and RegExp instances are cloned. other objects becomes Object objects, ie. Function, Error etc. non-enumerable properties, getters and setters are not copied
Create an object constructed using the enumerable properties of all provided arguments.var haraldutil = require('haraldutil')
console.log(haraldutil.merge({a: 1}, {a: 2, b: 2}, {c: 3}))
{ a: 2, b: 2, c: 3 }
Each frame in the frames array
return value: printable string
This function is similar to parseFloat, but does not accept trailing garbage characters.
convert Date to a unix timestamp number.Note that JavaScript does not provide daylight savings data for timezones, but it is implemented here of the US.
Encoding that allows for difference and comparison within a day for any time zoneThis allows to compare what is earlier in the day for another timezone than localtime or utc, using the commonly available utc timevalues.
Please suggest better ways, new features, and possible difficulties on github
© 2011, 2013 Harald Rudell wrote haraldutil for node in October, 2011