cw.lib object

Library contains several useful converters and utility functions. Also lib provides low-level hash and crypto functions.

Methods that return Promise are marked with ⚑ (black flag). 
Async methods with callbacks are marked with ▷(white triangle).

Converters

lib.base64( arg, decode )

If decode is false or none, converts anything into base64 string. If you pass an object, ensure it has no circular references.

If decode is true, first argument must be valid base64 string — and it will be decoded into source, with all functions, regexps and dates intact.

lib.blob2base64 (Blob, done, noMime) ▷

Async, callback done is called on finish. Converts Blob to base64, if noMime is true, result has no data:mime/type prefix.

lib.base642blob (base64, done, mime) ▷

Async, callback done is called on finish. Converts base64 string into Blob with mime content type.

lib.fromurl64( urlHash )

CloudWall reflects current app and its state to browser’s URL bar. App state is reflected to hash side of URL. It has 3 or 4 slash-separated parts.

lib.tourl64( arg )

Converts argument to URL params string. 

lib.image ( arg )

Image library. Allows to resize, sharpen or blur image. Applies brightness and contrast (or any other map function). Steps can be chained.

Argument is Image object or jQuery object, pointing to an image. Image must be loaded (load event fired).

CWImage object that is passed through, has several other members:

.width() is current stage image width
.height() is height
.ctx() is canvas context of current state
.convolute ( array ) convolves image with array
.destroy() kills CWImage object

Normally .jpeg(quality) or .png() calls destroy an object. To avoid destroying use .jpeg(quality, true) or .png(true). It’s helpful for generating multistep previews.

lib.js2txt ( jsObj, tab )

Renders javascript object as raw JS text. It’s not JSON but result string is eval()-compatible.

lib.json( any )

Converts argument to JSON. Functions, regexps and dates are preserved. Compliment function is cw.lib.unjson().

lib.unjson( object )

Converts object after JSON.stringify into full-featured object with functions, regexps and so on. Applied to unfold manifests.

Crypto and hashes

lib.enc ( arg, key )

Encodes arg (object, array, string, function or whatever) into OpenSSL AES256 salted packet. ‘Salted’ means you can encode same data twice with same key — result strings will differ.

Returns base64 string of a packet.

lib.dec( base64, key )

Decodes base64 string of an OpenSSL packet into source extended JSON object, string or whatever. Only AES256 salted packets are accepted. Complimentary method is cw.lib.enc().

lib.encAsync ( arg, key ) ⚑

Return Promise, resolved with encoded source. Async encoding of large objects is ~50 times faster than sync cw.lib.enc().

Encodes arg (object, array, string, function or whatever) into OpenSSL AES256 salted packet. ‘Salted’ means you can encode same data twice with same key — result strings will differ.

Returns base64 string of a packet.

lib.decAsync ( base64, key ) ⚑

Returns Promise, resolved with decoded source. Async decode is generally ~50 times faster then sync equivalent cw.lib.dec().

Decodes base64 string of an OpenSSL packet into source extended JSON object, string or whatever. Only AES256 salted packets are accepted. Complimentary method is cw.lib.encAsync().

lib.encDoc ( arg, key )

Same as cw.lib.enc(), but encodes only valid json-nable objects — so a little faster. Functions, regexps and so on are dropped.

lib.decDoc ( base64, key )

Decodes base64 string of an OpenSSL packet into source standard json object. Twice faster than .dec because operates on standard json, not extended. No functions or regexps are unfolded, they returned as source strings.

Only AES256 salted packets are accepted. Complimentary method is cw.lib.encDoc().

lib.sdbm( any )

Very fast hash based on sdbmHash used in early BerkeleyDB, return 8-9 chars. As it use standard JSON.stringify, all non-jsonnable properties of arg (if it’s an object) are dropped.

lib.crc2( any )

Fast key-sorting hash, based on sdbm hash. ‘Sorting’ means cw.lib.crc2({x:1,y:2}) will return same result as cw.lib.crc2({y:2,x:1}).

lib.footprint( docObj )

Returns 8-9 chars string with sdbm key-sorting hash of docObj, which is assumed to be valid CouchDB doc. Before processing, all first-level non-CouchDB fields starting with _ are dropped, also _attachments members are normalized.

lib.md5( any )

Returns MD5 hash of an argument.

lib.hash8( any )

Returns 8-chars 40-bit hash of an argument. Hash is derived from MD5, so it is quite slow but well-distributed. Use cw.lib.sdbm() instead if you need fast, but non-cryptographic hash.

lib.hash4( any )

Returns 4-chars 20-bit hash of an argument.

Utility functions

lib.a2o( Array )

Converts array of strings into object with keys that are unique array values, and each key’s value is counter.

lib.date( arg, fmt )

Formats date from arg (can be number, string or date) into human-comfort string, closer dates has better precision. fmt is string tinyshort or long. Default is short

Format is tiny:

Format is short:

Format is full:

lib.dehtml ( string, trim )

Removes all HTML markup from string except <a> tags. Trims string to trim chars with ellipsis. 

lib.dry ( obj, extraDry )

Returns shallow copy of an object with all first-level properties, starting with _ char, are removed. If extraDry is false, props _id_rev and _attachments are preserved.

lib.fuse( args )

Chain object merge, first argument is extended by subsequent ones in deep. It’s not like Object.merge(o1, o2, true) or $.extend(true, o1, o2, o3). Main difference is that arrays are cloned, but not merged.

lib.getref( obj, ref )

Returns object branch by string dot-notated reference. Unlike standard dot notation does not throw error when path does not exist.

lib.mask( obj, mask )

Mask source object with mask. Compliment is cw.lib.unmask()

lib.unmask( any, mask )

Unmasks masked object into source. Quite tricky, but understandable using examples. Utilized heavily in manifest and appcache engine.

When both args are objects, behaves same as cw.lib.mask().