Copy codeThe Code is as follows:
// Tool functions of the Hash object
Function $ H (object ){
Return new Hash (object );
};
Var Hash = Class. create (Enumerable, (function (){
// Initialize and create a new Hash object
Function initialize (object ){
This. _ object = Object. isHash (object )? Object. toObject (): Object. clone (object );
}
// Overwrite the methods in Enumerable, which will be used when traversing Hash objects
Function _ each (iterator ){
For (var key in this. _ object ){
Var value = this. _ object [key], pair = [key, value];
Pair. key = key;
Pair. value = value;
Iterator (pair );
}
}
Function set (key, value ){
Return this. _ object [key] = value;
}
Function get (key ){
If (this. _ object [key]! = Object. prototype [key])
Return this. _ object [key];
}
Function unset (key ){
Var value = this. _ object [key];
Delete this. _ object [key];
Return value;
}
Function toObject (){
Return Object. clone (this. _ object );
}
Function keys (){
Return this. pluck ('key ');
}
Function values (){
Return this. pluck ('value ');
}
// Return the key of the value
Function index (value ){
Var match = this. detect (function (pair ){
Return pair. value = value;
});
Return match & match. key;
}
Function merge (object ){
Return this. clone (). update (object );
}
// Update the original Hash object and update the key-value pairs in the object parameter to the original Hash object
Function update (object ){
Return new Hash (object). inject (this, function (result, pair ){
Result. set (pair. key, pair. value );
Return result;
});
}
Function toQueryPair (key, value ){
If (Object. isUndefined (value) return key;
Return key + '=' + encodeURIComponent (String. interpret (value ));
}
Function toQueryString (){
Return this. inject ([], function (results, pair ){
Var key = encodeURIComponent (pair. key), values = pair. value;
If (values & typeof values = 'object '){
If (Object. isArray (values ))
Return results. concat (values. map (toQueryPair. curry (key )));
} Else results. push (toQueryPair (key, values ));
Return results;
}). Join ('&');
}
Function inspect (){
Return '# <Hash: {' + this. map (function (pair ){
Return pair. map (Object. inspect). join (':');
}). Join (',') + '}> ';
}
Function toJSON (){
Return Object. toJSON (this. toObject ());
}
Function clone (){
Return new Hash (this );
}
Return {
Initialize: initialize,
_ Each: _ each,
Set: set,
Get: get,
Unset: unset,
ToObject: toObject,
ToTemplateReplacements: toObject,
Keys: keys,
Values: values,
Index: index,
Merge: merge,
Update: update,
ToQueryString: toQueryString,
Inspect: inspect,
ToJSON: toJSON,
Clone: clone
};
})());
Hash. from = $ H;
Clone
Each
Get
Inspect
Keys
Merge
Remove
Set
ToJSON
ToObject
ToQueryString
Unset
Update
Value
Some examples of methods are provided, and the simple method is skipped.
ToQueryString ():
Copy codeThe Code is as follows:
$ H ({action: 'ship ', order_id: 123, fees: ['f1', 'F2'], 'label': 'A' demo'}). toQueryString ()
//-> 'Action = ship & order_id = 123 & fees = f1 & fees = f2 & label = a % 20demo'
// An empty hash is an empty query string:
$ H (). toQueryString ()
//->''
Update ():
Copy codeThe Code is as follows:
Var h = $ H ({name: 'prototype', version: 1.5 });
H. update ({version: 1.6, author: 'Sam '}). inspect ();
//-> # <Hash: {'name': 'prototype', 'version': 1.6, 'author': 'Sam '}>
H. inspect ();
//-> # <Hash: {'name': 'prototype', 'version': 1.6, 'author': 'Sam '}>
// Note that the original Hash object will be changed here
Merge ():
Copy codeThe Code is as follows:
Var h = $ H ({name: 'prototype', version: 1.5 });
H. merge ({version: 1.6, author: 'Sam '}). inspect ();
//-> # <Hash: {'name': 'prototype', 'version': 1.6, 'author': 'Sam '}>
H. inspect ();
//-> # <Hash: {'name': 'prototype', 'version': 1.5}>
// Note that the original Hash object is not changed here