index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. (function (root, factory) {
  2. 'use strict'
  3. /*istanbul ignore next:cant test*/
  4. if (typeof module === 'object' && typeof module.exports === 'object') {
  5. module.exports = factory()
  6. } else if (typeof define === 'function' && define.amd) {
  7. // AMD. Register as an anonymous module.
  8. define([], factory)
  9. } else {
  10. // Browser globals
  11. root.objectPath = factory()
  12. }
  13. })(this, function () {
  14. 'use strict'
  15. var toStr = Object.prototype.toString
  16. function hasOwnProperty (obj, prop) {
  17. if (obj == null) {
  18. return false
  19. }
  20. //to handle objects with null prototypes (too edge case?)
  21. return Object.prototype.hasOwnProperty.call(obj, prop)
  22. }
  23. function isEmpty (value) {
  24. if (!value) {
  25. return true
  26. }
  27. if (isArray(value) && value.length === 0) {
  28. return true
  29. } else if (typeof value !== 'string') {
  30. for (var i in value) {
  31. if (hasOwnProperty(value, i)) {
  32. return false
  33. }
  34. }
  35. return true
  36. }
  37. return false
  38. }
  39. function toString (type) {
  40. return toStr.call(type)
  41. }
  42. function isObject (obj) {
  43. return typeof obj === 'object' && toString(obj) === '[object Object]'
  44. }
  45. var isArray = Array.isArray || function (obj) {
  46. /*istanbul ignore next:cant test*/
  47. return toStr.call(obj) === '[object Array]'
  48. }
  49. function isBoolean (obj) {
  50. return typeof obj === 'boolean' || toString(obj) === '[object Boolean]'
  51. }
  52. function getKey (key) {
  53. var intKey = parseInt(key)
  54. if (intKey.toString() === key) {
  55. return intKey
  56. }
  57. return key
  58. }
  59. function factory (options) {
  60. options = options || {}
  61. var objectPath = function (obj) {
  62. return Object.keys(objectPath).reduce(function (proxy, prop) {
  63. if (prop === 'create') {
  64. return proxy
  65. }
  66. /*istanbul ignore else*/
  67. if (typeof objectPath[prop] === 'function') {
  68. proxy[prop] = objectPath[prop].bind(objectPath, obj)
  69. }
  70. return proxy
  71. }, {})
  72. }
  73. var hasShallowProperty
  74. if (options.includeInheritedProps) {
  75. hasShallowProperty = function () {
  76. return true
  77. }
  78. } else {
  79. hasShallowProperty = function (obj, prop) {
  80. return (typeof prop === 'number' && Array.isArray(obj)) || hasOwnProperty(obj, prop)
  81. }
  82. }
  83. function getShallowProperty (obj, prop) {
  84. if (hasShallowProperty(obj, prop)) {
  85. return obj[prop]
  86. }
  87. }
  88. var getShallowPropertySafely
  89. if (options.includeInheritedProps) {
  90. getShallowPropertySafely = function (obj, currentPath) {
  91. if (typeof currentPath !== 'string' && typeof currentPath !== 'number') {
  92. currentPath = String(currentPath)
  93. }
  94. var currentValue = getShallowProperty(obj, currentPath)
  95. if (currentPath === '__proto__' || currentPath === 'prototype' ||
  96. (currentPath === 'constructor' && typeof currentValue === 'function')) {
  97. throw new Error('For security reasons, object\'s magic properties cannot be set')
  98. }
  99. return currentValue
  100. }
  101. } else {
  102. getShallowPropertySafely = function (obj, currentPath) {
  103. return getShallowProperty(obj, currentPath)
  104. }
  105. }
  106. function set (obj, path, value, doNotReplace) {
  107. if (typeof path === 'number') {
  108. path = [path]
  109. }
  110. if (!path || path.length === 0) {
  111. return obj
  112. }
  113. if (typeof path === 'string') {
  114. return set(obj, path.split('.').map(getKey), value, doNotReplace)
  115. }
  116. var currentPath = path[0]
  117. var currentValue = getShallowPropertySafely(obj, currentPath)
  118. if (path.length === 1) {
  119. if (currentValue === void 0 || !doNotReplace) {
  120. obj[currentPath] = value
  121. }
  122. return currentValue
  123. }
  124. if (currentValue === void 0) {
  125. //check if we assume an array
  126. if (typeof path[1] === 'number') {
  127. obj[currentPath] = []
  128. } else {
  129. obj[currentPath] = {}
  130. }
  131. }
  132. return set(obj[currentPath], path.slice(1), value, doNotReplace)
  133. }
  134. objectPath.has = function (obj, path) {
  135. if (typeof path === 'number') {
  136. path = [path]
  137. } else if (typeof path === 'string') {
  138. path = path.split('.')
  139. }
  140. if (!path || path.length === 0) {
  141. return !!obj
  142. }
  143. for (var i = 0; i < path.length; i++) {
  144. var j = getKey(path[i])
  145. if ((typeof j === 'number' && isArray(obj) && j < obj.length) ||
  146. (options.includeInheritedProps ? (j in Object(obj)) : hasOwnProperty(obj, j))) {
  147. obj = obj[j]
  148. } else {
  149. return false
  150. }
  151. }
  152. return true
  153. }
  154. objectPath.ensureExists = function (obj, path, value) {
  155. return set(obj, path, value, true)
  156. }
  157. objectPath.set = function (obj, path, value, doNotReplace) {
  158. return set(obj, path, value, doNotReplace)
  159. }
  160. objectPath.insert = function (obj, path, value, at) {
  161. var arr = objectPath.get(obj, path)
  162. at = ~~at
  163. if (!isArray(arr)) {
  164. arr = []
  165. objectPath.set(obj, path, arr)
  166. }
  167. arr.splice(at, 0, value)
  168. }
  169. objectPath.empty = function (obj, path) {
  170. if (isEmpty(path)) {
  171. return void 0
  172. }
  173. if (obj == null) {
  174. return void 0
  175. }
  176. var value, i
  177. if (!(value = objectPath.get(obj, path))) {
  178. return void 0
  179. }
  180. if (typeof value === 'string') {
  181. return objectPath.set(obj, path, '')
  182. } else if (isBoolean(value)) {
  183. return objectPath.set(obj, path, false)
  184. } else if (typeof value === 'number') {
  185. return objectPath.set(obj, path, 0)
  186. } else if (isArray(value)) {
  187. value.length = 0
  188. } else if (isObject(value)) {
  189. for (i in value) {
  190. if (hasShallowProperty(value, i)) {
  191. delete value[i]
  192. }
  193. }
  194. } else {
  195. return objectPath.set(obj, path, null)
  196. }
  197. }
  198. objectPath.push = function (obj, path /*, values */) {
  199. var arr = objectPath.get(obj, path)
  200. if (!isArray(arr)) {
  201. arr = []
  202. objectPath.set(obj, path, arr)
  203. }
  204. arr.push.apply(arr, Array.prototype.slice.call(arguments, 2))
  205. }
  206. objectPath.coalesce = function (obj, paths, defaultValue) {
  207. var value
  208. for (var i = 0, len = paths.length; i < len; i++) {
  209. if ((value = objectPath.get(obj, paths[i])) !== void 0) {
  210. return value
  211. }
  212. }
  213. return defaultValue
  214. }
  215. objectPath.get = function (obj, path, defaultValue) {
  216. if (typeof path === 'number') {
  217. path = [path]
  218. }
  219. if (!path || path.length === 0) {
  220. return obj
  221. }
  222. if (obj == null) {
  223. return defaultValue
  224. }
  225. if (typeof path === 'string') {
  226. return objectPath.get(obj, path.split('.'), defaultValue)
  227. }
  228. var currentPath = getKey(path[0])
  229. var nextObj = getShallowPropertySafely(obj, currentPath)
  230. if (nextObj === void 0) {
  231. return defaultValue
  232. }
  233. if (path.length === 1) {
  234. return nextObj
  235. }
  236. return objectPath.get(obj[currentPath], path.slice(1), defaultValue)
  237. }
  238. objectPath.del = function del (obj, path) {
  239. if (typeof path === 'number') {
  240. path = [path]
  241. }
  242. if (obj == null) {
  243. return obj
  244. }
  245. if (isEmpty(path)) {
  246. return obj
  247. }
  248. if (typeof path === 'string') {
  249. return objectPath.del(obj, path.split('.'))
  250. }
  251. var currentPath = getKey(path[0])
  252. getShallowPropertySafely(obj, currentPath)
  253. if (!hasShallowProperty(obj, currentPath)) {
  254. return obj
  255. }
  256. if (path.length === 1) {
  257. if (isArray(obj)) {
  258. obj.splice(currentPath, 1)
  259. } else {
  260. delete obj[currentPath]
  261. }
  262. } else {
  263. return objectPath.del(obj[currentPath], path.slice(1))
  264. }
  265. return obj
  266. }
  267. return objectPath
  268. }
  269. var mod = factory()
  270. mod.create = factory
  271. mod.withInheritedProps = factory({includeInheritedProps: true})
  272. return mod
  273. })