Вход Регистрация
Файл: jsonh/js/jsonh.js
Строк: 176
<?php
// both UPPER and lower, you choose
var JSONHjsonh JSONH = function (Array, JSON) {"use strict"// if you want

    /**
     * Copyright (C) 2011 by Andrea Giammarchi, @WebReflection
     * 
     * Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     * 
     * The above copyright notice and this permission notice shall be included in
     * all copies or substantial portions of the Software.
     * 
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     * THE SOFTWARE.
     */

    // transforms [{a:"A"},{a:"B"}] to [1,"a","A","B"]
    
function hpack(list) {
        for (var
            
length = list.length,
            
// defined properties (out of one object is enough)
            
keys Object_keys(length ? list[0] : {}),
            
klength keys.length,
            
// static length stack of JS values
            
result = Array(length klength),
            
0,
            
0,
            
kio;
            
length; ++i
        
) {
            for (
                
= list[i], ki 0;
                
ki klength;
                
result[j++] = o[keys[ki++]]
            );
        }
        
// keys.length, keys, result
        
return concat.call([klength], keysresult);
    }

    
// transforms [1,"a","A","B"] to [{a:"A"},{a:"B"}]
    
function hunpack(hlist) {
        for (var
            
length hlist.length,
            
klength hlist[0],
            
result = Array(((length klength 1) / klength) || 0),
            
klength,
            
0,
            
kio;
            
length;
        ) {
            for (
                
result[j++] = (= {}), ki 0;
                
ki klength;
                
o[hlist[++ki]] = hlist[i++]
            );
        }
        return 
result;
    }

    
// recursive: called via map per each item h(pack|unpack)ing each entry through the schema
    
function iteratingWith(method) {
        return function 
iterate(item) {
            for (var
                
path this,
                
current item,
                
0length path.length,
                
jktmp;
                
length; ++i
            
) {
                if (
isArray(tmp current[path[i]])) {
                    
1;
                    
current[k] = length ?
                        
map.call(tmpmethodpath.slice(j)) :
                        
method(tmp)
                    ;
                }
                
current current[k];
            }
            return 
item;
        };
    }

    
// called per each schema (pack|unpack)ing each schema
    
function packOrUnpack(method) {
        return function 
parse(oschema) {
            for (var
                
wasArray isArray(o),
                
result concat.call(arro),
                
path concat.call(arrschema),
                
0length path.length;
                
length; ++i
            
) {
                
result map.call(resultmethodpath[i].split("."));
            }
            return 
wasArray result result[0];
        };
    }

    
// JSONH.pack
    
function pack(list, schema) {
        return 
schema packSchema(list, schema) : hpack(list);
    }

    
// JSONH unpack
    
function unpack(hlistschema) {
        return 
schema unpackSchema(hlistschema) : hunpack(hlist);
    }

    
// JSON.stringify after JSONH.pack
    
function stringify(list, replacerspaceschema) {
        return 
JSON_stringify(pack(list, schema), replacerspace);
    }

    
// JSONH.unpack after JSON.parse
    
function parse(hlistreviverschema) {
        return 
unpack(JSON_parse(hlistreviver), schema);
    }

    var
        
// recycled for different operations
        
arr = [],
        
// trapped once reused forever
        
concat arr.concat,
        
// addressed cross platform Object.keys shim
        
Object_keys Object.keys || function (o) {
            var 
keys = [], key;
            for (
key in oo.hasOwnProperty(key) && keys.push(key);
            return 
keys;
        },
        
// addressed cross platform Array.isArray shim
        
isArray = Array.isArray || (function (toStringarrayToString) {
            
arrayToString toString.call(arr);
            return function 
isArray(o) {
                return 
toString.call(o) == arrayToString;
            };
        }({}.
toString)),
        
// fast and partial Array#map shim
        
map arr.map || function (callbackcontext) {
            for (var
                
self thisself.lengthresult = Array(i);
                
i--;
                
result[i] = callback.call(contextself[i], iself)
            );
            return 
result;
        },
        
// schema related (pack|unpack)ing operations
        
packSchema packOrUnpack(iteratingWith(hpack)),
        
unpackSchema packOrUnpack(iteratingWith(hunpack)),
        
// JSON object shortcuts
        
JSON_stringify JSON.stringify,
        
JSON_parse JSON.parse
    
;

    return {
        
packpack,
        
parseparse,
        
stringifystringify,
        
unpackunpack
    
};

}(Array, 
JSON);

// export for node.js
if(typeof module != 'undefined' && module.exports) {
    
module.exports jsonh;
}
?>
Онлайн: 0
Реклама