Skip to content

Commit

Permalink
rename qibl, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasq committed Apr 10, 2019
1 parent ee3d556 commit bed994e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
62 changes: 37 additions & 25 deletions README.md
@@ -1,55 +1,63 @@
qpoly
=====
qibl
====

Polyfills that I at times I wished I had, and wrote.
Quick Itty-Bitty Library.

qpoly = require('qpoly');
A miscellaneous collection of small functions and polyfills I wrote that that I found myself
needing again, gathered into a single place. Most are pretty efficient, at times faster even
than the equivalent built-in.

qibl = require('qibl');


API
---

### qpoly.isHash( object )
### 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.

### qpoly.copyObject( target, src1, ... )
### qibl.copyObject( target, src1, ... )

Assign all enumerable own properties of the sources `src` onto `target`, and return
`target`. Similar to `Object.assign`.
`target`. Equivalent to `Object.assign`.

### qpoly.merge( target, src1, ... )
### 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`.

### qpoly.fill( buf, ch [,base] [,bound] )
### qibl.fill( buf, ch [,base] [,bound] )

Fill the buffer or array with the value `ch` from starting offset `base` and up to the limit
`bound` (but not including `bound`).

### qpoly.str_repeat( str, n )
### 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.
Similar to `String.prototype.repeat`.
Equivalent to `String.prototype.repeat`.

### qpoly.newBuf( arg, encodingOrOffset, length )
### qibl.newBuf( arg, encodingOrOffset, length )

Construct a Buffer like `new Buffer()` used to before it was deprecated.

### qpoly.allocBuf( length )
### qibl.allocBuf( length )

Create a new Buffer having the given length, with contents uninitialized.
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.

### qpoly.fromBuf( contents )
### 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.

### qpoly.toStruct( hash )
### 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
Expand All @@ -59,13 +67,13 @@ Accessing an object can over time result in it being optimized for mapped lookup
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.

### qpoly.varargs( handler(argv, self) [,self] )
### qibl.varargs( handler(argv, self) [,self] )

Return a function that when called will in turn call handler with all its arguments in an
array. This functionality is no longer really needed with ES6 rest args, but is useful for
portability. It is not slower than rest args.

### qpoly.thunkify( func [,self] )
### 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
Expand All @@ -79,37 +87,41 @@ For example, given a function `fn(a, b, cb)`:
}
}

### qpoly.invoke( fn, argv )
### qibl.invoke( fn, argv )

Call the function with the given argument vector.

### qpoly.invoke2( fn, self, argv )
### qibl.invoke2( fn, self, argv )

Call the method on the object `self` with the given argument vector.

### qpoly.escapeRegex( str )
### qibl.escapeRegex( str )

Backslash-escape all characters in str that would act as metacharacters inside a regular
expression. Returns the string with escapes added.

### qpoly.values( object )
### qibl.values( object )

Return an array with the own properties of the object. Similar to `Object.values`.
Return an array with the own properties of the object. Equivalent to `Object.values`.

### 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.

### qpoly.vinterpolate( string, substring, argv [,addslashes] )
function selectField( array, name ) {
return array.map((item) => item[name]);
}

### 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!"

### qpoly.addslashes( str [,regex] )
### 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).
Expand Down
1 change: 1 addition & 0 deletions qibl.js
Expand Up @@ -104,6 +104,7 @@ function saneBuf( ) {
return {
new: eval('nodeMajor < 10 ? Buffer : function(a, b, c) { return typeof(a) === "number" ? Buffer.allocUnsafe(a) : Buffer.from(a, b, c) }'),
alloc: eval('nodeMajor >= 6 ? Buffer.allocUnsafe : Buffer'),
// allocFill: function(n, ch) { var buf = qibl.allocBuf(n); if (ch !== undefined) qibl.fill(buf, ch); return buf },
from: eval('nodeMajor >= 6 ? Buffer.from : Buffer'),
}
}
Expand Down

0 comments on commit bed994e

Please sign in to comment.