localforage.js
// we store binary data with localStorage.
we store binary data with localStorage.
we store binary data with localStorage .
function _serialize(value, callback) {
        var valueString = '';
        if (value) {
            valueString = value.toString();
        }
         TODO: See why those tests fail and use a better solution.
        if (value && (value.toString() === '[object ArrayBuffer]' ||
                      value.buffer && value.buffer.toString() === '[object ArrayBuffer]')) {
             Convert binary arrays to a string and prefix the string with
             a special marker.
            var buffer;
            var marker = SERIALIZED_MARKER;
            if (value instanceof ArrayBuffer) {
                buffer = value;
                marker += TYPE_ARRAYBUFFER;
            } else {
                buffer = value.buffer;
                if (valueString === '[object Int8Array]') {
                    marker += TYPE_INT8ARRAY;
                } else if (valueString === '[object Uint8Array]') {
                    marker += TYPE_UINT8ARRAY;
                } else if (valueString === '[object Uint8ClampedArray]') {
                    marker += TYPE_UINT8CLAMPEDARRAY;
                } else if (valueString === '[object Int16Array]') {
                    marker += TYPE_INT16ARRAY;
                } else if (valueString === '[object Uint16Array]') {
                    marker += TYPE_UINT16ARRAY;
                } else if (valueString === '[object Int32Array]') {
                    marker += TYPE_INT32ARRAY;
                } else if (valueString === '[object Uint32Array]') {
                    marker += TYPE_UINT32ARRAY;
                } else if (valueString === '[object Float32Array]') {
                    marker += TYPE_FLOAT32ARRAY;
                } else if (valueString === '[object Float64Array]') {
                    marker += TYPE_FLOAT64ARRAY;
                } else {
                    callback(new Error("Failed to get type for BinaryArray"));
                }
            }
            callback(marker + _bufferToString(buffer));
        } else if (valueString === "[object Blob]") {
             Conver the blob to a binaryArray and then to a string.
            var fileReader = new FileReader();
            fileReader.onload = function() {
                var str = _bufferToString(this.result);
                callback(SERIALIZED_MARKER + TYPE_BLOB + str);
            };
            fileReader.readAsArrayBuffer(value);
        } else {
            try {
                callback(JSON.stringify(value));
            } catch (e) {
                if (window.console && window.console.error) {
                    window.console.error("Couldn't convert value into a JSON string: ", value);
                }
                callback(null, e);
            }
        }
    }
function _serialize ( value , callback ) { var valueString = '' ; if ( value ) { valueString = value . toString ( ) ; } if ( value && ( value . toString ( ) === '[object ArrayBuffer]' || value . buffer && value . buffer . toString ( ) === '[object ArrayBuffer]' ) ) { var buffer ; var marker = SERIALIZED_MARKER ; if ( value instanceof ArrayBuffer ) { buffer = value ; marker += TYPE_ARRAYBUFFER ; } else { buffer = value . buffer ; if ( valueString === '[object Int8Array]' ) { marker += TYPE_INT8ARRAY ; } else if ( valueString === '[object Uint8Array]' ) { marker += TYPE_UINT8ARRAY ; } else if ( valueString === '[object Uint8ClampedArray]' ) { marker += TYPE_UINT8CLAMPEDARRAY ; } else if ( valueString === '[object Int16Array]' ) { marker += TYPE_INT16ARRAY ; } else if ( valueString === '[object Uint16Array]' ) { marker += TYPE_UINT16ARRAY ; } else if ( valueString === '[object Int32Array]' ) { marker += TYPE_INT32ARRAY ; } else if ( valueString === '[object Uint32Array]' ) { marker += TYPE_UINT32ARRAY ; } else if ( valueString === '[object Float32Array]' ) { marker += TYPE_FLOAT32ARRAY ; } else if ( valueString === '[object Float64Array]' ) { marker += TYPE_FLOAT64ARRAY ; } else { callback ( new Error ( "Failed to get type for BinaryArray" ) ) ; } } callback ( marker + _bufferToString ( buffer ) ) ; } else if ( valueString === "[object Blob]" ) { var fileReader = new FileReader ( ) ; fileReader . onload = function ( ) { var str = _bufferToString ( this . result ) ; callback ( SERIALIZED_MARKER + TYPE_BLOB + str ) ; } ; fileReader . readAsArrayBuffer ( value ) ; } else { try { callback ( JSON . stringify ( value ) ) ; } catch ( e ) { if ( window . console && window . console . error ) { window . console . error ( "Couldn't convert value into a JSON string: " , value ) ; } callback ( null , e ) ; } } }
we store binary data with localStorage.
we store binary data with localStorage .
// Cannot use `value instanceof ArrayBuffer` or such here, as these // checks fail when running the tests using casper.js... // // TODO: See why those tests fail and use a better solution. // Convert binary arrays to a string and prefix the string with // a special marker. // Conver the blob to a binaryArray and then to a string.