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
+12 -13
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