/
eStruct.js
120 lines (103 loc) · 3.23 KB
/
eStruct.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
** eStruct **
JavaScript function to handle hash tables
Copyright (c) 2012 Ervin Kosch, released under the GPL 3 license
https://github.com/tekrat/eDrop
*/
function eStruct(){
this.structure = {};
// Add(Key Name, Value) - Add a key/value pair
this.Add = function(key, value){
this.structure[key] = value;
}
// IsEmpty() - Returns true if there no key/value pairs
this.IsEmpty = function(){
return (this.Len() == 0);
}
// KeyExists(Key Name) - Does this key exists in the object?
this.KeyExists = function(key){
return this.structure.hasOwnProperty(key);
}
// KeyList() - Returns an array with all keys in the object as they were added
this.KeyList = function(){
var ReturnValue = new Array();
for (var key in this.structure) {
ReturnValue.push(key);
}
return ReturnValue;
}
// KeyListSort(Sort Direction) - Returns an array with all keys in the structure
// sorted by your preference: 1 - Ascending (Default), 0 - Descending
this.KeyListSort = function(SortDirection){
var ReturnValue = this.KeyList();
if(SortDirection == 0){
return ReturnValue.sort().reverse();
}else{
return ReturnValue.sort();
}
}
// ValueList() - Returns an array with all values in the structure as they were added
this.ValueList = function(){
var ReturnValue = new Array();
for (var key in this.structure) {
ReturnValue.push(this.structure[key]);
}
return ReturnValue;
}
// KeyListSort(Sort Direction) - Returns an array with all values in the structure
// sorted by your preference: 1 - Ascending (Default), 0 - Descending
this.ValueListSort = function(SortDirection){
var ReturnValue = this.ValueList();
if(SortDirection == 0){
return ReturnValue.sort().reverse();
}else{
return ReturnValue.sort();
}
}
// Len() - Returns the number of items in the structure
this.Len = function(){
var ReturnValue = 0;
for (var k in this.structure) {
ReturnValue++;
}
return ReturnValue;
}
// Get(Key Name) - Returns the value of a stucture item,
// returns NULL if the key cannot be found
this.Get = function (key){
if( this.KeyExists(key) ){
return this.structure[key];
}else{
return null;
}
}
// Delete(Key Name) - Removes a specfic key/item pair from the structure
this.Delete = function(key){
if( this.KeyExists(key) ){
delete this.structure[key]
}else{
return undefined;
}
}
// Clear() - Removes everything out of the structure
this.Clear = function(){
for (var key in this.structure) {
this.Delete(key);
}
}
// GetStruct() - Returns the raw structure
this.GetStruct = function(){
return this.structure;
}
// Serialize() - Returns an XML string
this.Serialize = function(){
var ReturnValue = "<struct>\r\n";
for (var k in this.structure) {
ReturnValue += " <" + k + ">" +
encodeURIComponent(this.structure[k]) +
"</" + k + ">\r\n";
}
ReturnValue += "</struct>";
return ReturnValue;
}
}