-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring, fixes.. all decoder tests passed!
- Loading branch information
Showing
10 changed files
with
468 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var mainAssert = require('assert'); | ||
var Map = require('./map'); | ||
var Entity = require('./entity'); | ||
|
||
module.exports.equal = function (expected, actual) { | ||
var mapToObject = function (value) { | ||
if (value instanceof Map) { | ||
var obj = {}; | ||
value.forEach(function (key, value) { | ||
obj[key] = mapToObject(value); | ||
}); | ||
return obj; | ||
} else if (value instanceof Entity) { | ||
value.attributes = mapToObject(value.attributes); | ||
value.value = mapToObject(value.value); | ||
} | ||
|
||
return value; | ||
}; | ||
return mainAssert.deepEqual(expected, mapToObject(actual)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
var Entity = require('./entity'); | ||
|
||
function Map() { | ||
this._items = []; | ||
this._indexes = {}; | ||
this._key = 0; | ||
this.length = 0; | ||
|
||
this.set = function (key, value, replace) { | ||
if (typeof replace === "undefined") { | ||
replace = true; | ||
} | ||
if (key === null) { | ||
key = this._key++; | ||
} else { | ||
var number = parseInt(key * 1); | ||
if (!isNaN(number) && number > this._key) { | ||
this._key = number; | ||
} | ||
} | ||
if (typeof this._indexes[key] === "undefined") { | ||
this._items[this.length] = {_key: key, value: value}; | ||
this._indexes[key] = this.length; | ||
this.length++; | ||
return 1; | ||
} else if (replace) { | ||
this._items[this._indexes[key]].value = value; | ||
return 0; | ||
} else { | ||
return -1; | ||
} | ||
}; | ||
|
||
this.add = function (key, value) { | ||
return this.set(key, value, false) === 1; | ||
}; | ||
|
||
this.get = function (key) { | ||
if (!this.has(key)) { | ||
throw new Error("Item with key " + key + " not exists"); | ||
} | ||
return this._items[this._indexes[key]].value; | ||
}; | ||
|
||
this.last = function() { | ||
if(this.length === 0) { | ||
throw new Error("Map has no items"); | ||
} | ||
return this._items[this.length - 1]; | ||
}; | ||
|
||
this.has = function (key) { | ||
return typeof this._indexes[key] !== "undefined"; | ||
}; | ||
|
||
this.remove = function (key) { | ||
if (!this.has(key)) { | ||
throw new Error("Item with key " + key + " not exists"); | ||
} | ||
delete this._items[this._indexes[key]]; | ||
delete this._indexes[key]; | ||
this.length--; | ||
}; | ||
|
||
this.items = function () { | ||
return this._items; | ||
}; | ||
|
||
this.keys = function () { | ||
var keys = []; | ||
for (var i in this._items) { | ||
keys.push(this._items[i]._key); | ||
} | ||
return keys; | ||
}; | ||
|
||
this.values = function () { | ||
var values = []; | ||
for (var i in this._items) { | ||
values.push(this._items[i].value); | ||
} | ||
return values; | ||
}; | ||
|
||
this.toObject = function (deep) { | ||
if (typeof deep === "undefined") { | ||
deep = false; | ||
} | ||
var result = {}; | ||
for (var i in this._items) { | ||
var value = this._items[i].value; | ||
result[this._items[i]._key] = value instanceof Map && deep ? value.toObject(true) : value; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
this.forEach = function (callable) { | ||
for (var i in this._items) { | ||
callable(this._items[i]._key, this._items[i].value); | ||
} | ||
}; | ||
|
||
|
||
} | ||
Map.fromObject = function (obj) { | ||
var mapInst = new Map; | ||
for (var key in obj) { | ||
mapInst.set(key, obj[key]); | ||
} | ||
return mapInst; | ||
}; | ||
|
||
module.exports = Map; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
//var encoder = require("./encoder"); | ||
var EncoderClass = require("./encoder"); | ||
var DecoderClass = require("./decoder"); | ||
var encode = function (xvar, options) { | ||
if (typeof options === "undefined") { | ||
options = null; | ||
} | ||
|
||
var encoder = new encoder(); | ||
var encoder = new EncoderClass(); | ||
return encoder.encode(xvar, options); | ||
}; | ||
|
||
|
||
module.exports.encode = encode; | ||
module.exports.decode = function (input) { | ||
var decoder = new (require("./decoder"))(); | ||
var decoder = new DecoderClass(); | ||
|
||
return decoder.decode(input); | ||
}; | ||
module.exports.Entity = require('./entity').Entity; | ||
module.exports.Entity = require('./entity'); | ||
module.exports.Map = require('./map'); | ||
module.exports.CHAIN = require('./decoder').CHAIN; |
Oops, something went wrong.