1
- 'use strict' ;
2
-
3
1
/* !
4
2
* Chai - pathval utility
5
3
* Copyright(c) 2012-2014 Jake Luer <[email protected] >
44
42
* @api public
45
43
*/
46
44
47
- function hasProperty ( obj , name ) {
45
+ export function hasProperty ( obj , name ) {
48
46
if ( typeof obj === 'undefined' || obj === null ) {
49
47
return false ;
50
48
}
@@ -73,19 +71,19 @@ function hasProperty(obj, name) {
73
71
*/
74
72
75
73
function parsePath ( path ) {
76
- var str = path . replace ( / ( [ ^ \\ ] ) \[ / g, '$1.[' ) ;
77
- var parts = str . match ( / ( \\ \. | [ ^ . ] + ?) + / g) ;
78
- return parts . map ( function mapMatches ( value ) {
74
+ const str = path . replace ( / ( [ ^ \\ ] ) \[ / g, '$1.[' ) ;
75
+ const parts = str . match ( / ( \\ \. | [ ^ . ] + ?) + / g) ;
76
+ return parts . map ( ( value ) => {
79
77
if (
80
78
value === 'constructor' ||
81
79
value === '__proto__' ||
82
80
value === 'prototype'
83
81
) {
84
82
return { } ;
85
83
}
86
- var regexp = / ^ \[ ( \d + ) \] $ / ;
87
- var mArr = regexp . exec ( value ) ;
88
- var parsed = null ;
84
+ const regexp = / ^ \[ ( \d + ) \] $ / ;
85
+ const mArr = regexp . exec ( value ) ;
86
+ let parsed = null ;
89
87
if ( mArr ) {
90
88
parsed = { i : parseFloat ( mArr [ 1 ] ) } ;
91
89
} else {
@@ -112,12 +110,12 @@ function parsePath(path) {
112
110
*/
113
111
114
112
function internalGetPathValue ( obj , parsed , pathDepth ) {
115
- var temporaryValue = obj ;
116
- var res = null ;
113
+ let temporaryValue = obj ;
114
+ let res = null ;
117
115
pathDepth = typeof pathDepth === 'undefined' ? parsed . length : pathDepth ;
118
116
119
- for ( var i = 0 ; i < pathDepth ; i ++ ) {
120
- var part = parsed [ i ] ;
117
+ for ( let i = 0 ; i < pathDepth ; i ++ ) {
118
+ const part = parsed [ i ] ;
121
119
if ( temporaryValue ) {
122
120
if ( typeof part . p === 'undefined' ) {
123
121
temporaryValue = temporaryValue [ part . i ] ;
@@ -149,13 +147,13 @@ function internalGetPathValue(obj, parsed, pathDepth) {
149
147
*/
150
148
151
149
function internalSetPathValue ( obj , val , parsed ) {
152
- var tempObj = obj ;
153
- var pathDepth = parsed . length ;
154
- var part = null ;
150
+ let tempObj = obj ;
151
+ const pathDepth = parsed . length ;
152
+ let part = null ;
155
153
// Here we iterate through every part of the path
156
- for ( var i = 0 ; i < pathDepth ; i ++ ) {
157
- var propName = null ;
158
- var propVal = null ;
154
+ for ( let i = 0 ; i < pathDepth ; i ++ ) {
155
+ let propName = null ;
156
+ let propVal = null ;
159
157
part = parsed [ i ] ;
160
158
161
159
// If it's the last part of the path, we set the 'propName' value with the property name
@@ -169,7 +167,7 @@ function internalSetPathValue(obj, val, parsed) {
169
167
tempObj = tempObj [ part . i ] ;
170
168
} else {
171
169
// If the obj doesn't have the property we create one with that name to define it
172
- var next = parsed [ i + 1 ] ;
170
+ const next = parsed [ i + 1 ] ;
173
171
// Here we set the name of the property which will be defined
174
172
propName = typeof part . p === 'undefined' ? part . i : part . p ;
175
173
// Here we decide if this property will be an array or a new object
@@ -202,10 +200,10 @@ function internalSetPathValue(obj, val, parsed) {
202
200
* @api public
203
201
*/
204
202
205
- function getPathInfo ( obj , path ) {
206
- var parsed = parsePath ( path ) ;
207
- var last = parsed [ parsed . length - 1 ] ;
208
- var info = {
203
+ export function getPathInfo ( obj , path ) {
204
+ const parsed = parsePath ( path ) ;
205
+ const last = parsed [ parsed . length - 1 ] ;
206
+ const info = {
209
207
parent :
210
208
parsed . length > 1 ?
211
209
internalGetPathValue ( obj , parsed , parsed . length - 1 ) :
@@ -249,8 +247,8 @@ function getPathInfo(obj, path) {
249
247
* @api public
250
248
*/
251
249
252
- function getPathValue ( obj , path ) {
253
- var info = getPathInfo ( obj , path ) ;
250
+ export function getPathValue ( obj , path ) {
251
+ const info = getPathInfo ( obj , path ) ;
254
252
return info . value ;
255
253
}
256
254
@@ -287,15 +285,8 @@ function getPathValue(obj, path) {
287
285
* @api private
288
286
*/
289
287
290
- function setPathValue ( obj , path , val ) {
291
- var parsed = parsePath ( path ) ;
288
+ export function setPathValue ( obj , path , val ) {
289
+ const parsed = parsePath ( path ) ;
292
290
internalSetPathValue ( obj , val , parsed ) ;
293
291
return obj ;
294
292
}
295
-
296
- module . exports = {
297
- hasProperty : hasProperty ,
298
- getPathInfo : getPathInfo ,
299
- getPathValue : getPathValue ,
300
- setPathValue : setPathValue ,
301
- } ;
0 commit comments