Members
LS
More convenient version of localStorage.
- Source:
Methods
ago_str()
Returns a human readable relative time description for a Unix timestmap versus "now"
E.M. agoStr( time() - 60 ) // "60 seconds ago"
E.M. agoStr( time() - 63 ) // "1 minute ago"
Pass a truthy value for argument 'no_suffix' to suppress the " ago" at the end
- Source:
b2c()
An alias for bucksToCents().
- Source:
- See:
bucksToCents(bucks) → {number}
Convert from dollars to pennies.
Note: This is different from simply multiplying by 100, in that
the arg is passed through toFlt(), so you can pass in any object
and it will try to do a sensible thing with it.
Parameters:
| Name | Type | Description |
|---|---|---|
bucks |
number | A float - dollars and cents |
- Source:
- See:
Returns:
- a whole number of cents
- Type
- number
Example
bucksToCents( 1.23 ) // 123
bucksToCents( "1.23" ) // 123
bucksToCents( [1.23] ) // 0
bucksToCents( NaN ) // 0
byteSize(sz) → {string}
Returns a human readable string that describes 'n' as a number of bytes,
"1 KB", "21.5 MB", etc.
Parameters:
| Name | Type | Description |
|---|---|---|
sz |
number |
- Source:
Returns:
- Type
- string
c2b()
An alias for centsToBucks().
- Source:
- See:
centsToBucks(cents) → {number}
Convert from pennies to dollars.
Note: This is different from simply dividing by 100, in that
the arg is passed through toFlt(), so you can pass in any object
and it will try to do a sensible thing with it.
Parameters:
| Name | Type | Description |
|---|---|---|
cents |
number | A number of pennies |
- Source:
- See:
Returns:
- a number fixed to 2 decimals
- Type
- number
Example
centsToBucks( 123 ) // 1.23
centsToBucks( "123" ) // 1.23
centsToBucks( [123] ) // 0.0
centsToBucks( NaN ) // 0.0
dt2ts()
Convert Date object to Unix timestamp
- Source:
file_info() → {object}
Get fs stat object.
If cb provided, do it asyncrhonously and call cb
NOTE: This is non-browser only function
- Source:
Returns:
- fs.Stats object or null if error (ENOENT)
- Type
- object
get_file(path, enc, cb) → {string}
Fetch the contents of a file.
In the browser this is done by with and HTTP GET request to the server.
In Node, fs.readFile()/readfileSync() is used.
Parameters:
| Name | Type | Description |
|---|---|---|
path |
string | |
enc |
string | |
cb |
function |
- Source:
Returns:
- only if using Node, and only if no callback is provided
- Type
- string
isBrowser()
Returns true if we appear to be running in a browser (as opposed to Node, etc.)
- Source:
is_dir() → {boolean}
Returns true if path is a directory.
NOTE: This is non-browser only function
- Source:
Returns:
- Type
- boolean
is_file() → {boolean}
Returns true if path is a file.
NOTE: This is non-browser only function
- Source:
Returns:
- Type
- boolean
j2o(j) → {varies}
Convert JSON string to object or primitive.
CAUTION: there is no way to distinguish between this function encountering an error
in parsing and a successful conversion of the string "null" to null.
Parameters:
| Name | Type | Description |
|---|---|---|
j |
sting | The string to be converted |
- Source:
- See:
Returns:
- The output of JSON.parse() or null if it threw an Error
- Type
- varies
log()
An alias for console.log.
Behaves identically.
- Source:
my2ts()
Convert "YYYY-MM-YY" or "YYYY-MM-YY HH:MM:SS" to Unix timestamp
- Source:
numFmt(n, plcs, dot, sep) → {string}
Format a number into a string with any # of decimal places,
and optional alternative decimal & thousand-separation chars
Parameters:
| Name | Type | Description |
|---|---|---|
n |
number | the number to format |
plcs |
number | decimal places |
dot |
string | decimal point separator |
sep |
string | thousands separator |
- Source:
Returns:
- Type
- string
Example
numFmt( 1234.56 ) // "1,235"
numFmt( 1234.56, 1 ) // "1,234.6"
numFmt( 1234.56, 1, "," ) // "1,234,6"
numFmt( 1234.56, 1, "_" ) // "1,234_6"
numFmt( 1234.56, 1, ",", "." ) // "1.234,6"
numFmt( 1234.56, 1, ".", "" ) // "1234.6"
o2j(v) → {string}
Convert an object to JSON text without throwing.
Parameters:
| Name | Type | Description |
|---|---|---|
v |
any | The object to converted. |
- Source:
- See:
Returns:
JSON string, or null if JSON.stringify() threw an Error.
- Type
- string
query_data(key) → {string}
Returns an object constructed from the current page's query args,
or, if an argument is provided, just a single value for key.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string | key to return |
- Source:
Returns:
- Type
- string
Example
query_data() // { page: "home", foo: "bar" }
query_data( "page" ) // "home"
rand_hash() → {string}
Returns a pretty randomish sha1 hash.
NOTE: This is non-browser only function in that it calls sha1()
- Source:
- See:
Returns:
- Type
- string
runp()
Run some functions in parallel / simultaneously
runp() (no args) is DEPRECATED
- Source:
runq()
Run some functions sequentially / synchronously ( see test.js )
runq() (no args) is DEPRECATED
- Source:
sha1(s) → {string}
Return ASCII sha1 string for a string
Parameters:
| Name | Type | Description |
|---|---|---|
s |
string |
- Source:
- See:
Returns:
- Type
- string
sha256(s) → {string}
Return ASCII sha256 string for a string
Parameters:
| Name | Type | Description |
|---|---|---|
s |
string |
- Source:
- See:
Returns:
- Type
- string
t2h()
Alias for text2html()
- Source:
- See:
text2html(t) → {string}
Sort of like Markdown, but not really.
Parameters:
| Name | Type | Description |
|---|---|---|
t |
string |
- Source:
- See:
Returns:
- Type
- string
throwIf(c, s)
Throws an Error if a condition is true.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
c |
any | that which is tested for truthiness | |
s |
string | FAILED ASSERTION | Message displayd by the Error object if condition is true |
- Source:
time()
Return a Unix timestamp for current time, or for a Date object if provided
- Source:
toFlt(v) → {number}
Convert whatever to float or 0 if not at all number-like.
CAUTION: This is not very smart; It just coerces to string, then strips out extraneous chars
Parameters:
| Name | Type | Description |
|---|---|---|
v |
any | What you want to convert |
- Source:
Returns:
- Type
- number
Example
toFlt( "123.9" ) // 123.9
toFlt( null ) // 0.0
toFlt( undefined ) // 0.0
toFlt( NaN ) // 0.0
toFlt( 123.9 ) // 123.9
toInt(v) → {number}
Convert whatever to integer or 0 if not at all number-like.
CAUTION: This is not very smart; It just coerces to string, then strips out extraneous chars
CAUTION: Uses Math.round()
Parameters:
| Name | Type | Description |
|---|---|---|
v |
any | What you want to convert |
- Source:
Returns:
- an integer
- Type
- number
Example
toFlt( "123.9" ) // 123
toFlt( null ) // 0
toFlt( undefined ) // 0
toFlt( NaN ) // 0
toFlt( 123.9 ) // 124
toMoney(n, dot, sep) → {string}
Convert whatever to a string that looks like "1,234.56"
Add the $ symbol yourself.
Parameters:
| Name | Type | Description |
|---|---|---|
n |
number | the number to format |
dot |
string | decimal point separator |
sep |
string | thousands separator |
- Source:
Returns:
- Type
- string
Example
toMoney( 1234.56 ) // "1,234.56"
toMoney( 1234.56, 1, ".", "" ) // "1.234,56"
toPct(n, plcs, dot, sep) → {string}
Convert fraction to percent.
convert something like 0.12 to a string that looks like "12" with
optional alternate decimal and thousands-seperator chars
NOTE: there is no "%" added, you have to do that yourself if you want it.
Parameters:
| Name | Type | Description |
|---|---|---|
n |
number | the number to format |
plcs |
number | decimal places |
dot |
string | decimal point separator |
sep |
string | thousands separator |
- Source:
Returns:
- Type
- string
Example
toPct( 0.4 ) + "%" // "40%"
toPct( 123.4,",", "." ) // "12,340"
ts2dt()
Convert Unix timestamp to Date object
Returns null (NOT a date object for "now" as you might expect) if ts is falsey.
- Source:
ts2my()
Convert Unix timestamp to "YYYY-MM-DD HH:MM:SS"
- Source:
ts2us()
Convert Unix timestamp to "MM/DD/YYYY HH:MM:SS" or "" if ts is 0
- Source:
ts2us_dMy()
Convert Unix timestamp to something like "01-Jan-2016" or "" if ts is 0
- Source:
ts2us_hm()
Convert Unix timestamp to "HH:MM" or "" if ts is 0
- Source:
ts2us_md()
Convert Unix timestamp to "MM/DD" or "" if ts is 0
- Source:
ts2us_mdy()
Convert Unix timestamp to "MM/DD/YYYY" or "" if ts is 0
- Source:
ts2us_mdy2()
Convert Unix timestamp to "MM/DD/YY" or "" if ts is 0
- Source:
ts2us_mdy2hm()
Convert Unix timestamp to "MM/DD/YY HH:MM" or "" if ts is 0
- Source:
ts2us_mdyhm()
Convert Unix timestamp to "MM/DD/YYYY HH:MM" or "" if ts is 0
- Source:
us2dt()
Convert "MM/DD/YYYY HH:MM:SS" to Date object or null if string can't be parsed
If year is 2 digits, it will try guess the century (not recommended).
Time part (HH:MM:SS) can be omitted and seconds is optional
if utc argument is truthy, then return a UTC version
- Source:
us2ts()
Convert "MM/DD/YYYY HH:MM:SS" to Unix timestamp.
If utc argument is truthy, then return a UTC version.
- Source: