Kyoloro / compress-str

:checkered_flag: a javascript module for node.js to compress string with "zlib"

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Compress-Str

Base on "zlib" module to compress string - base64 for database saving (redis or mongodb ...). Support both callback and native Promise

Build status node-image

Notice

gzip function is a high CPU consumption, not support to use at high I/O case.

Installation

$ npm install compress-str

Example

'use strict'

var compress = require('compress-str')
var str = 'this_is_a_very_very_very_very_very_very_very_very_very_very_very_very_long_string'

console.log(str.length) // 85

// for callback
compress.gzip(str, function (err, m) {
    if (err) {
        console.error(err)
    } else {
        console.log(m.length) // 45

        compress.gunzip(m, function (err, n) {
            if (err) {
                console.error(err)
            } else {
                console.log(n.length) // 85
                console.log(n === str) // true
            }
        })
    }
})

// for Promise
compress.gzip(str).then(function (m) {
    console.log(m.length) // 45
    return compress.gunzip(m)
}).then(function (n) {
    console.log(n.length) // 85
    console.log(n === str) // true
}).catch(function (err) {
    console.error(err)
})

FAQ

this module use native Promise for node.js v0.12 +, when your node.js version can't support Promise, you may use bluebird or do something yourself with callback

'use strict'

var compress = require('compress-str')
var Promise = require('bluebird')

compress.Promise = Promise
// to do can do well

API

.gzip(string || object[, callback])

gzip string (return a Promise when missing callback)

.gunzip(string[, callback])

gunzip string from gzip (return a Promise when missing callback)

License

MIT

About

:checkered_flag: a javascript module for node.js to compress string with "zlib"

License:MIT License


Languages

Language:JavaScript 100.0%