Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- some progress

  • Loading branch information...
commit 4f410558369112c194735d33e77cc75ba6435eab 1 parent 7eddd8d
Nickolay Platonov authored
4 Build.PL
View
@@ -5,10 +5,10 @@ name Data.Visitor
version 0.01
author 'Nickolay (SamuraiJack) Platonov <root@symbie.org>'
-abstract 'Some clever yet compact description'
+abstract 'Visitor style traversal of JavaScript data structures'
license lgpl
-requires Joose 3.01
+requires Joose 3.001
docs_markup mmd
53 lib/Data/Visitor.js
View
@@ -2,8 +2,6 @@ Class('Data.Visitor', {
has : {
- ID : null,
-
seen : Joose.I.Object
},
@@ -14,8 +12,6 @@ Class('Data.Visitor', {
var res = []
var seen = this.seen
-
-
Joose.A.each(arguments, function (value) {
if (value && typeof value == 'object' || typeof value == 'function') {
@@ -26,12 +22,12 @@ Class('Data.Visitor', {
if (seen[ref])
res.push(this.visitSeen(value))
- else {
-
- }
+ else
+ res.push(this.visitNotSeen(value))
} else
res.push(this.visitValue(value))
+
}, this)
if (res.length > 1) return res
@@ -47,19 +43,30 @@ Class('Data.Visitor', {
visitNotSeen : function (value) {
- value.__VISITOR__ = []
+ return this.seen[ value.__REF__ ] = this.visitObject(value)
+ },
+
+
+ visitArray : function (array) {
+ Joose.A.each(array, function (value, index) {
+
+ this.visitArrayEntry(value, index, array)
+
+ }, this)
+ return array
},
- visitArray : function () {
+ visitArrayEntry : function (value, index, array) {
+ return this.visit(value)
},
visitObject : function (value) {
if (Joose.O.isInstance(value)) return this.visitInstance(value)
- if (value instanceof array) return this.visitArray(value)
+ if (value instanceof Array) return this.visitArray(value)
if (typeof value == 'function') return this.visitFunction(value)
@@ -73,12 +80,17 @@ Class('Data.Visitor', {
},
- visitGenericObject : function (value) {
- Joose.O.eachOwn(value, function (value, key) {
-
+ visitGenericObject : function (object) {
+ Joose.O.eachOwn(object, function (value, key) {
+ if (key != '__REF__') {
+ this.visitObjectKey(key, value, object)
+ this.visitObjectValue(value, key, object)
+ }
}, this)
+
+ return object
},
@@ -87,15 +99,18 @@ Class('Data.Visitor', {
},
- visitObjectKey : function () {
+ visitObjectKey : function (key, value, object) {
+ return this.visitValue(key)
},
- visitObjectValue : function () {
+ visitObjectValue : function (value, key, object) {
+ return this.visit(value)
},
- visitFunction : function () {
+ visitFunction : function (value) {
+ return this.visitValue(value)
}
},
@@ -109,7 +124,7 @@ body : function () {
my : {
has : {
- hostClass : null,
+ HOST : null,
tidy : true
},
@@ -123,9 +138,9 @@ body : function () {
visit : function () {
- var visitor = new this.hostClass()
+ var visitor = new this.HOST()
- visitor.visit.apply(visitor, arguments)
+ return visitor.visit.apply(visitor, arguments)
}
}
}
2  lib/Data/Visitor.mmd
View
@@ -2,7 +2,7 @@ Name
====
-Data.Visitor.Class - Some clever yet compact description
+Data.Visitor - Visitor style traversal of JavaScript data structures
SYNOPSIS
7 t/010_sanity.t.js → t/010_basics.t.js
View
@@ -11,6 +11,13 @@ StartTest(function(t) {
t.ok(Data.Visitor, "Data.Visitor is here")
+
+ var data1 = {}
+ var data2 = []
+
+ t.ok(data1 == Data.Visitor.my.visit(data1), "Visiting don't modify the data #1")
+ t.ok(data2 == Data.Visitor.my.visit(data2), "Visiting don't modify the data #2")
+
t.endAsync(async0)
})
})
5 t/index.html
View
@@ -44,13 +44,14 @@
'/jsan/Task/JooseX/Namespace/Depended/Web.js',
{
text : "JooseX.Namespace.Depended.Manager.my.INC = " + Ext.encode(INC)
- }
+ },
+ 'Data.Visitor'
]
})
Test.Run.Harness.Browser.Multi.my.start(
- '010_sanity.t.js'
+ '010_basics.t.js'
)
</script>
</body>
Please sign in to comment.
Something went wrong with that request. Please try again.