File tree Expand file tree Collapse file tree 3 files changed +36
-7
lines changed Expand file tree Collapse file tree 3 files changed +36
-7
lines changed Original file line number Diff line number Diff line change @@ -84,3 +84,14 @@ exports.scan = function (obj, options) {
84
84
}
85
85
}
86
86
} ;
87
+
88
+
89
+ exports . safeParse = function ( text , reviver ) {
90
+
91
+ try {
92
+ return exports . parse ( text , reviver ) ;
93
+ }
94
+ catch ( ignoreError ) {
95
+ return null ;
96
+ }
97
+ } ;
Original file line number Diff line number Diff line change 12
12
],
13
13
"dependencies" : {},
14
14
"devDependencies" : {
15
- "benchmark " : " ^2.1.4 " ,
16
- "code " : " 5 .x.x" ,
17
- "lab " : " 18.x.x "
15
+ "@hapi/code " : " 5.x.x " ,
16
+ "@hapi/lab " : " 18 .x.x" ,
17
+ "benchmark " : " ^2.1.4 "
18
18
},
19
19
"scripts" : {
20
- "test" : " lab -a code -t 100 -L" ,
21
- "test-cov-html" : " lab -a code -r html -o coverage.html"
20
+ "test" : " lab -a @hapi/ code -t 100 -L" ,
21
+ "test-cov-html" : " lab -a @hapi/ code -r html -o coverage.html"
22
22
},
23
23
"license" : " BSD-3-Clause"
24
24
}
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
- const Code = require ( 'code' ) ;
3
+ const Code = require ( '@hapi/ code' ) ;
4
4
const Bourne = require ( '..' ) ;
5
- const Lab = require ( 'lab' ) ;
5
+ const Lab = require ( '@hapi/ lab' ) ;
6
6
7
7
8
8
const internals = { } ;
@@ -137,4 +137,22 @@ describe('Bourne', () => {
137
137
expect ( obj ) . to . equal ( { a : 5 , b : 6 , hasOwnProperty : 'text' } ) ;
138
138
} ) ;
139
139
} ) ;
140
+
141
+ describe ( 'safeParse()' , ( ) => {
142
+
143
+ it ( 'parses object string' , ( ) => {
144
+
145
+ expect ( Bourne . safeParse ( '{"a": 5, "b": 6}' ) ) . to . equal ( { a : 5 , b : 6 } ) ;
146
+ } ) ;
147
+
148
+ it ( 'returns null on proto object string' , ( ) => {
149
+
150
+ expect ( Bourne . safeParse ( '{ "a": 5, "b": 6, "__proto__": { "x": 7 } }' ) ) . to . be . null ( ) ;
151
+ } ) ;
152
+
153
+ it ( 'returns null on invalid object string' , ( ) => {
154
+
155
+ expect ( Bourne . safeParse ( '{"a": 5, "b": 6' ) ) . to . be . null ( ) ;
156
+ } ) ;
157
+ } ) ;
140
158
} ) ;
You can’t perform that action at this time.
0 commit comments