diff options
| author | mo <mo.khan@gmail.com> | 2017-09-03 18:58:18 -0600 |
|---|---|---|
| committer | mo <mo.khan@gmail.com> | 2017-09-03 18:58:18 -0600 |
| commit | 168716b78cf4d128d73885f875c4bb6d03caf06b (patch) | |
| tree | 54198367847248cfad999dacdd55363a08ec123b /app/assets/javascripts/lib | |
| parent | 8863c9d09c4b2be5516e97406b09fa460386f8fa (diff) | |
add class to resolve object from path.
Diffstat (limited to 'app/assets/javascripts/lib')
| -rw-r--r-- | app/assets/javascripts/lib/proxy.js.coffee | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/proxy.js.coffee b/app/assets/javascripts/lib/proxy.js.coffee new file mode 100644 index 00000000..a99b907d --- /dev/null +++ b/app/assets/javascripts/lib/proxy.js.coffee @@ -0,0 +1,31 @@ +class CakeSide.Proxy + @create: do -> + parse = (object, path) -> + return unless CakeSide.Proxy.typeOf(object) == 'object' and path? + return [object, path] if path.indexOf('.') == -1 + path = path.split('.') + tail = path.pop() + for property in path + return unless object.hasOwnProperty(property) + object = object[property] + [object, tail] + + read = (object, path) -> + parts = parse(object, path) + return parts[0][parts[1]] if parts + + write = (object, path, value) -> + parts = parse(object, path) + if parts + parts[0][parts[1]] = value + value + + (object, path, value) -> + return read(object, path) if arguments.length == 2 + return write(object, path, value) if arguments.length == 3 + console.error '[CakeSide.Proxy.create] incorrect number of arguments' + + @typeOf: (object) -> + return 'array' if _.isArray(object) + return 'regexp' if _.isRegExp(object) + typeof(object) |
