refactor(dotSet): move dotSet & dotGet to exports

This commit is contained in:
Tyler Renelle 2014-07-17 12:55:45 -06:00
parent b449387cc0
commit f1db2bf1d5
2 changed files with 36 additions and 29 deletions

View file

@ -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;

View file

@ -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