qibl v1.24.0
qibl
Quick Itty-Bitty Library.
A miscellaneous collection of small functions and polyfills I wrote that that I found myself reusing, gathered into a single place. Most are pretty efficient, at times faster even than the equivalent built-in.
To run the tests, check out the repo.
qibl = require('qibl');
API
Objects
qibl.isHash( object )
Test whether the object is a generic hash {}
ie new Object()
, or is an instance of some
class. Tests the object constructor.
qibl.copyObject( target, src1, ... )
Assign all enumerable own properties of the sources src
onto target
, and return
target
. Equivalent to Object.assign
.
qibl.merge( target, src1, ... )
Recursively copy all enumerable properties of the source objects, including inherited properties, onto
the target
object. All nested hashes are copied onto a new hash {}
so the target
will not share any sub-object with any of the sources. Non-hash objects (ie instances of
classes other than Object
) are assigned by value. Returns the target
.
qibl.toStruct( hash )
Convert the object from hashed accesses to an optimized mapped accesses analogous to C
struct
s. This exposes a hidden internal language detail: V8 optimizes objects with a static
layout for more efficient access.
Accessing an object can over time result in it being optimized for mapped lookups or optimized for hashed lookups, but making an object into a prototype forces an immediate conversion to mapped lookups. To retain the speedup, do not add new properties.
qibl.selectField( arrayOfObjects, propertyName )
Return an array with the values of the named property from each object in the input
array. The value is undefined
if the property is not set.
function selectField( array, name ) {
return array.map((item) => item[name]);
}
qibl.values( object )
Return an array with the own properties of the object. Equivalent to Object.values
.
Strings
qibl.str_repeat( str, n )
Repeat the string value str
n
times. N should be non-negative, else node will run out
of memory. Uses an efficient O(log n) string doubling approach. Returns the repeated string.
Equivalent to String.prototype.repeat
.
qibl.escapeRegex( str )
Backslash-escape all characters in str that would act as metacharacters inside a regular expression. Returns the string with escapes added.
qibl.vinterpolate( string, substring, argv ,addslashes )
Replace each occurrence of the substring in string with the next argument in the vector argv. Substrings without a corresponding argument are not replaced.
vinterpolate("Hello, %s!", '%s', ["world"]);
// => "Hello, world!"
qibl.addslashes( str ,regex )
Backslash-escape characters in the string. Without a regex, the characters escaped by default are ', ", \ and \0 (single-quote, double-quote, backslash and NUL).
If a regex is passed in, the patterns matched by its first capturing group will be the ones escaped instead.
addslashes("curl test.com/;cat /etc/passwd", /([;|&$])/g);
// => "curl test.com/\;cat /etc/passwd"
Buffers and Arrays
qibl.fill( buf, ch ,base )
Fill the buffer or array with the value ch
from starting offset base
and up to the limit
bound
(but not including bound
).
qibl.newBuf( arg, encodingOrOffset, length )
Construct a Buffer like new Buffer()
used to before it was deprecated. Note that with
newer node the constructor polyfill is slow compared to the allocBuf
and fromBuf
builders.
qibl.allocBuf( length )
Create a new Buffer having the given length, with contents uninitialized. This builder is a
pass-through to the native implementation (Buffer.allocUnsafe
or Buffer
) and always runs
at full speed.
qibl.fromBuf( contents )
Create a new Buffer with its contents pre-initialized to the given string, array or buffer.
This builder is a pass-through to the native implementation (Buffer.from
or Buffer
) and
always runs at full speed.
Functions
qibl.varargs( handler(argv, self) ,self )
Return a function that when called will in turn call handler with all its arguments in an array (an "argument vector"). This functionality is no longer really needed with ES6 rest args, but is useful for portability. It is not slower than rest args.
qibl.invoke( fn, argv )
Call the function with the given argument vector.
qibl.invoke2( fn, self, argv )
Call the method on the object self
with the given argument vector.
qibl.thunkify( func ,self )
Split the method into two functions, one (the thunk
) to partially apply the
function to the arguments and to return the other (the invoke
) that runs the
function when called with a callback. thunkify
returns the thunk.
For example, given a function fn(a, b, cb)
:
function thunk(a, b) {
return function invoke(cb) {
return fn(a, b, cb);
}
}