Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pathval.js 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.pathval = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. 'use strict';
  3. /* !
  4. * Chai - pathval utility
  5. * Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
  6. * @see https://github.com/logicalparadox/filtr
  7. * MIT Licensed
  8. */
  9. /**
  10. * ### .hasProperty(object, name)
  11. *
  12. * This allows checking whether an object has own
  13. * or inherited from prototype chain named property.
  14. *
  15. * Basically does the same thing as the `in`
  16. * operator but works properly with null/undefined values
  17. * and other primitives.
  18. *
  19. * var obj = {
  20. * arr: ['a', 'b', 'c']
  21. * , str: 'Hello'
  22. * }
  23. *
  24. * The following would be the results.
  25. *
  26. * hasProperty(obj, 'str'); // true
  27. * hasProperty(obj, 'constructor'); // true
  28. * hasProperty(obj, 'bar'); // false
  29. *
  30. * hasProperty(obj.str, 'length'); // true
  31. * hasProperty(obj.str, 1); // true
  32. * hasProperty(obj.str, 5); // false
  33. *
  34. * hasProperty(obj.arr, 'length'); // true
  35. * hasProperty(obj.arr, 2); // true
  36. * hasProperty(obj.arr, 3); // false
  37. *
  38. * @param {Object} object
  39. * @param {String|Symbol} name
  40. * @returns {Boolean} whether it exists
  41. * @namespace Utils
  42. * @name hasProperty
  43. * @api public
  44. */
  45. function hasProperty(obj, name) {
  46. if (typeof obj === 'undefined' || obj === null) {
  47. return false;
  48. }
  49. // The `in` operator does not work with primitives.
  50. return name in Object(obj);
  51. }
  52. /* !
  53. * ## parsePath(path)
  54. *
  55. * Helper function used to parse string object
  56. * paths. Use in conjunction with `internalGetPathValue`.
  57. *
  58. * var parsed = parsePath('myobject.property.subprop');
  59. *
  60. * ### Paths:
  61. *
  62. * * Can be infinitely deep and nested.
  63. * * Arrays are also valid using the formal `myobject.document[3].property`.
  64. * * Literal dots and brackets (not delimiter) must be backslash-escaped.
  65. *
  66. * @param {String} path
  67. * @returns {Object} parsed
  68. * @api private
  69. */
  70. function parsePath(path) {
  71. var str = path.replace(/([^\\])\[/g, '$1.[');
  72. var parts = str.match(/(\\\.|[^.]+?)+/g);
  73. return parts.map(function mapMatches(value) {
  74. var regexp = /^\[(\d+)\]$/;
  75. var mArr = regexp.exec(value);
  76. var parsed = null;
  77. if (mArr) {
  78. parsed = { i: parseFloat(mArr[1]) };
  79. } else {
  80. parsed = { p: value.replace(/\\([.\[\]])/g, '$1') };
  81. }
  82. return parsed;
  83. });
  84. }
  85. /* !
  86. * ## internalGetPathValue(obj, parsed[, pathDepth])
  87. *
  88. * Helper companion function for `.parsePath` that returns
  89. * the value located at the parsed address.
  90. *
  91. * var value = getPathValue(obj, parsed);
  92. *
  93. * @param {Object} object to search against
  94. * @param {Object} parsed definition from `parsePath`.
  95. * @param {Number} depth (nesting level) of the property we want to retrieve
  96. * @returns {Object|Undefined} value
  97. * @api private
  98. */
  99. function internalGetPathValue(obj, parsed, pathDepth) {
  100. var temporaryValue = obj;
  101. var res = null;
  102. pathDepth = (typeof pathDepth === 'undefined' ? parsed.length : pathDepth);
  103. for (var i = 0; i < pathDepth; i++) {
  104. var part = parsed[i];
  105. if (temporaryValue) {
  106. if (typeof part.p === 'undefined') {
  107. temporaryValue = temporaryValue[part.i];
  108. } else {
  109. temporaryValue = temporaryValue[part.p];
  110. }
  111. if (i === (pathDepth - 1)) {
  112. res = temporaryValue;
  113. }
  114. }
  115. }
  116. return res;
  117. }
  118. /* !
  119. * ## internalSetPathValue(obj, value, parsed)
  120. *
  121. * Companion function for `parsePath` that sets
  122. * the value located at a parsed address.
  123. *
  124. * internalSetPathValue(obj, 'value', parsed);
  125. *
  126. * @param {Object} object to search and define on
  127. * @param {*} value to use upon set
  128. * @param {Object} parsed definition from `parsePath`
  129. * @api private
  130. */
  131. function internalSetPathValue(obj, val, parsed) {
  132. var tempObj = obj;
  133. var pathDepth = parsed.length;
  134. var part = null;
  135. // Here we iterate through every part of the path
  136. for (var i = 0; i < pathDepth; i++) {
  137. var propName = null;
  138. var propVal = null;
  139. part = parsed[i];
  140. // If it's the last part of the path, we set the 'propName' value with the property name
  141. if (i === (pathDepth - 1)) {
  142. propName = typeof part.p === 'undefined' ? part.i : part.p;
  143. // Now we set the property with the name held by 'propName' on object with the desired val
  144. tempObj[propName] = val;
  145. } else if (typeof part.p !== 'undefined' && tempObj[part.p]) {
  146. tempObj = tempObj[part.p];
  147. } else if (typeof part.i !== 'undefined' && tempObj[part.i]) {
  148. tempObj = tempObj[part.i];
  149. } else {
  150. // If the obj doesn't have the property we create one with that name to define it
  151. var next = parsed[i + 1];
  152. // Here we set the name of the property which will be defined
  153. propName = typeof part.p === 'undefined' ? part.i : part.p;
  154. // Here we decide if this property will be an array or a new object
  155. propVal = typeof next.p === 'undefined' ? [] : {};
  156. tempObj[propName] = propVal;
  157. tempObj = tempObj[propName];
  158. }
  159. }
  160. }
  161. /**
  162. * ### .getPathInfo(object, path)
  163. *
  164. * This allows the retrieval of property info in an
  165. * object given a string path.
  166. *
  167. * The path info consists of an object with the
  168. * following properties:
  169. *
  170. * * parent - The parent object of the property referenced by `path`
  171. * * name - The name of the final property, a number if it was an array indexer
  172. * * value - The value of the property, if it exists, otherwise `undefined`
  173. * * exists - Whether the property exists or not
  174. *
  175. * @param {Object} object
  176. * @param {String} path
  177. * @returns {Object} info
  178. * @namespace Utils
  179. * @name getPathInfo
  180. * @api public
  181. */
  182. function getPathInfo(obj, path) {
  183. var parsed = parsePath(path);
  184. var last = parsed[parsed.length - 1];
  185. var info = {
  186. parent: parsed.length > 1 ? internalGetPathValue(obj, parsed, parsed.length - 1) : obj,
  187. name: last.p || last.i,
  188. value: internalGetPathValue(obj, parsed),
  189. };
  190. info.exists = hasProperty(info.parent, info.name);
  191. return info;
  192. }
  193. /**
  194. * ### .getPathValue(object, path)
  195. *
  196. * This allows the retrieval of values in an
  197. * object given a string path.
  198. *
  199. * var obj = {
  200. * prop1: {
  201. * arr: ['a', 'b', 'c']
  202. * , str: 'Hello'
  203. * }
  204. * , prop2: {
  205. * arr: [ { nested: 'Universe' } ]
  206. * , str: 'Hello again!'
  207. * }
  208. * }
  209. *
  210. * The following would be the results.
  211. *
  212. * getPathValue(obj, 'prop1.str'); // Hello
  213. * getPathValue(obj, 'prop1.att[2]'); // b
  214. * getPathValue(obj, 'prop2.arr[0].nested'); // Universe
  215. *
  216. * @param {Object} object
  217. * @param {String} path
  218. * @returns {Object} value or `undefined`
  219. * @namespace Utils
  220. * @name getPathValue
  221. * @api public
  222. */
  223. function getPathValue(obj, path) {
  224. var info = getPathInfo(obj, path);
  225. return info.value;
  226. }
  227. /**
  228. * ### .setPathValue(object, path, value)
  229. *
  230. * Define the value in an object at a given string path.
  231. *
  232. * ```js
  233. * var obj = {
  234. * prop1: {
  235. * arr: ['a', 'b', 'c']
  236. * , str: 'Hello'
  237. * }
  238. * , prop2: {
  239. * arr: [ { nested: 'Universe' } ]
  240. * , str: 'Hello again!'
  241. * }
  242. * };
  243. * ```
  244. *
  245. * The following would be acceptable.
  246. *
  247. * ```js
  248. * var properties = require('tea-properties');
  249. * properties.set(obj, 'prop1.str', 'Hello Universe!');
  250. * properties.set(obj, 'prop1.arr[2]', 'B');
  251. * properties.set(obj, 'prop2.arr[0].nested.value', { hello: 'universe' });
  252. * ```
  253. *
  254. * @param {Object} object
  255. * @param {String} path
  256. * @param {Mixed} value
  257. * @api private
  258. */
  259. function setPathValue(obj, path, val) {
  260. var parsed = parsePath(path);
  261. internalSetPathValue(obj, val, parsed);
  262. return obj;
  263. }
  264. module.exports = {
  265. hasProperty: hasProperty,
  266. getPathInfo: getPathInfo,
  267. getPathValue: getPathValue,
  268. setPathValue: setPathValue,
  269. };
  270. },{}]},{},[1])(1)
  271. });