From f1db2bf1d565a8feb6d6ac910de02893e4b58889 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Thu, 17 Jul 2014 12:55:45 -0600 Subject: [PATCH] refactor(dotSet): move dotSet & dotGet to exports --- dist/habitrpg-shared.js | 40 ++++++++++++++++++++++++---------------- script/index.coffee | 25 ++++++++++++------------- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/dist/habitrpg-shared.js b/dist/habitrpg-shared.js index 4799789f44..b99586619d 100644 --- a/dist/habitrpg-shared.js +++ b/dist/habitrpg-shared.js @@ -12306,10 +12306,31 @@ api = module.exports = {}; api.i18n = i18n; -$w = function(s) { +$w = api.$w = function(s) { return s.split(' '); }; +api.dotSet = function(obj, path, val) { + var arr; + arr = path.split('.'); + return _.reduce(arr, (function(_this) { + return function(curr, next, index) { + if ((arr.length - 1) === index) { + curr[next] = val; + } + return curr[next] != null ? curr[next] : curr[next] = {}; + }; + })(this), obj); +}; + +api.dotGet = function(obj, path) { + return _.reduce(path.split('.'), ((function(_this) { + return function(curr, next) { + return curr != null ? curr[next] : void 0; + }; + })(this)), obj); +}; + /* ------------------------------------------------------ @@ -13802,23 +13823,10 @@ api.wrap = function(user, main) { Angular sets object properties directly - in which case, this function will be used. */ dotSet: function(path, val) { - var arr; - arr = path.split('.'); - return _.reduce(arr, (function(_this) { - return function(curr, next, index) { - if ((arr.length - 1) === index) { - curr[next] = val; - } - return curr[next] != null ? curr[next] : curr[next] = {}; - }; - })(this), user); + return api.dotSet(user, path, val); }, dotGet: function(path) { - return _.reduce(path.split('.'), ((function(_this) { - return function(curr, next) { - return curr != null ? curr[next] : void 0; - }; - })(this)), user); + return api.dotGet(user, path); }, randomDrop: function(modifiers, req) { var acceptableDrops, chance, drop, dropK, quest, rarity, task, _base, _base1, _base2, _name, _name1, _name2, _ref, _ref1; diff --git a/script/index.coffee b/script/index.coffee index 27d06c29d4..33f1ea66d6 100644 --- a/script/index.coffee +++ b/script/index.coffee @@ -7,7 +7,16 @@ api = module.exports = {} api.i18n = i18n # little helper for large arrays of strings. %w"this that another" equivalent from Rails, I really miss that function -$w = (s)->s.split(' ') +$w = api.$w = (s)->s.split(' ') +api.dotSet = (obj,path,val)-> + arr = path.split('.') + _.reduce arr, (curr, next, index) => + if (arr.length - 1) == index + curr[next] = val + (curr[next] ?= {}) + , obj +api.dotGet = (obj,path)-> + _.reduce path.split('.'), ((curr, next) => curr?[next]), obj ### ------------------------------------------------------ @@ -975,18 +984,8 @@ api.wrap = (user, main=true) -> so that different consumers can implement setters their own way. Derby needs model.set(path, value) for example, where Angular sets object properties directly - in which case, this function will be used. ### - dotSet: (path, val) -> - arr = path.split('.') - _.reduce arr, (curr, next, index) => - if (arr.length - 1) == index - curr[next] = val - (curr[next] ?= {}) - , user - - dotGet: (path) -> - _.reduce path.split('.'), ((curr, next) => curr?[next]), user - - + dotSet: (path, val)-> api.dotSet user,path,val + dotGet: (path)-> api.dotGet user,path # ---------------------------------------------------------------------- # Scoring