@@ -5,11 +5,10 @@ const isString = require('lodash/isString')
55const cloneDeep = require ( 'lodash/cloneDeep' )
66const isPlainObject = require ( 'lodash/isPlainObject' )
77const jsonpointer = require ( 'json-pointer' )
8- const { DataPackageError} = require ( './errors' )
8+ const { DataPackageError } = require ( './errors' )
99const config = require ( './config' )
1010const omit = require ( 'lodash/omit' )
1111
12-
1312// Locate descriptor
1413
1514function locateDescriptor ( descriptor ) {
@@ -19,23 +18,21 @@ function locateDescriptor(descriptor) {
1918 if ( isString ( descriptor ) ) {
2019 basePath = descriptor . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) || '.'
2120
22- // Current dir by default
21+ // Current dir by default
2322 } else {
2423 basePath = '.'
2524 }
2625
2726 return basePath
2827}
2928
30-
3129// Retrieve descriptor
3230
3331async function retrieveDescriptor ( descriptor ) {
3432 if ( isPlainObject ( descriptor ) ) {
3533 return cloneDeep ( descriptor )
3634 }
3735 if ( isString ( descriptor ) ) {
38-
3936 // Remote
4037 if ( isRemotePath ( descriptor ) ) {
4138 try {
@@ -46,7 +43,7 @@ async function retrieveDescriptor(descriptor) {
4643 throw new DataPackageError ( message )
4744 }
4845
49- // Local
46+ // Local
5047 } else {
5148 if ( config . IS_BROWSER ) {
5249 const message = `Local descriptor "${ descriptor } " in browser is not supported`
@@ -61,25 +58,25 @@ async function retrieveDescriptor(descriptor) {
6158 throw new DataPackageError ( message )
6259 }
6360 }
64-
6561 }
6662 throw new DataPackageError ( 'Descriptor must be String or Object' )
6763}
6864
69-
7065// Dereference descriptor
7166
7267async function dereferencePackageDescriptor ( descriptor , basePath ) {
7368 descriptor = cloneDeep ( descriptor )
7469 for ( const [ index , resource ] of ( descriptor . resources || [ ] ) . entries ( ) ) {
7570 // TODO: May be we should use Promise.all here
7671 descriptor . resources [ index ] = await dereferenceResourceDescriptor (
77- resource , basePath , descriptor )
72+ resource ,
73+ basePath ,
74+ descriptor
75+ )
7876 }
7977 return descriptor
8078}
8179
82-
8380async function dereferenceResourceDescriptor ( descriptor , basePath , baseDescriptor ) {
8481 descriptor = cloneDeep ( descriptor )
8582 baseDescriptor = baseDescriptor || descriptor
@@ -91,7 +88,7 @@ async function dereferenceResourceDescriptor(descriptor, basePath, baseDescripto
9188 if ( ! isString ( value ) ) {
9289 continue
9390
94- // URI -> Pointer
91+ // URI -> Pointer
9592 } else if ( value . startsWith ( '#' ) ) {
9693 try {
9794 descriptor [ property ] = jsonpointer . get ( baseDescriptor , value . slice ( 1 ) )
@@ -100,7 +97,7 @@ async function dereferenceResourceDescriptor(descriptor, basePath, baseDescripto
10097 throw new DataPackageError ( message )
10198 }
10299
103- // URI -> Remote
100+ // URI -> Remote
104101 } else {
105102 if ( basePath && isRemotePath ( basePath ) ) {
106103 // TODO: support other that Unix OS
@@ -115,7 +112,7 @@ async function dereferenceResourceDescriptor(descriptor, basePath, baseDescripto
115112 throw new DataPackageError ( message )
116113 }
117114
118- // URI -> Local
115+ // URI -> Local
119116 } else {
120117 if ( config . IS_BROWSER ) {
121118 const message = 'Local URI dereferencing in browser is not supported'
@@ -146,7 +143,6 @@ async function dereferenceResourceDescriptor(descriptor, basePath, baseDescripto
146143 return descriptor
147144}
148145
149-
150146// Expand descriptor
151147
152148function expandPackageDescriptor ( descriptor ) {
@@ -158,17 +154,15 @@ function expandPackageDescriptor(descriptor) {
158154 return descriptor
159155}
160156
161-
162157function expandResourceDescriptor ( descriptor ) {
163158 descriptor = cloneDeep ( descriptor )
164159 descriptor . profile = descriptor . profile || config . DEFAULT_RESOURCE_PROFILE
165160 descriptor . encoding = descriptor . encoding || config . DEFAULT_RESOURCE_ENCODING
166161 if ( descriptor . profile === 'tabular-data-resource' ) {
167-
168162 // Schema
169163 const schema = descriptor . schema
170164 if ( schema !== undefined ) {
171- for ( const field of ( schema . fields || [ ] ) ) {
165+ for ( const field of schema . fields || [ ] ) {
172166 field . type = field . type || config . DEFAULT_FIELD_TYPE
173167 field . format = field . format || config . DEFAULT_FIELD_FORMAT
174168 }
@@ -192,14 +186,18 @@ function expandResourceDescriptor(descriptor) {
192186
193187// quoteChar and escapeChar are mutually exclusive: https://frictionlessdata.io/specs/csv-dialect/#specification
194188function filterDefaultDialect ( dialect = { } ) {
195- const defaultDialects = dialect . hasOwnProperty ( 'escapeChar' ) ? omit ( config . DEFAULT_DIALECT , 'quoteChar' ) : config . DEFAULT_DIALECT
189+ const defaultDialects = dialect . hasOwnProperty ( 'escapeChar' )
190+ ? omit ( config . DEFAULT_DIALECT , 'quoteChar' )
191+ : config . DEFAULT_DIALECT
196192 return defaultDialects
197193}
198194
199195// quoteChar and escapeChar are mutually exclusive: https://frictionlessdata.io/specs/csv-dialect/#specification
200196function validateDialect ( dialect = { } ) {
201197 if ( dialect . hasOwnProperty ( 'escapeChar' ) && dialect . hasOwnProperty ( 'quoteChar' ) ) {
202- throw new DataPackageError ( 'Resource.table dialect options quoteChar and escapeChar are mutually exclusive.' )
198+ throw new DataPackageError (
199+ 'Resource.table dialect options quoteChar and escapeChar are mutually exclusive.'
200+ )
203201 }
204202 return dialect
205203}
@@ -209,10 +207,9 @@ function isRemotePath(path) {
209207 return path . startsWith ( 'http' )
210208}
211209
212-
213210function isSafePath ( path ) {
214- const containsWindowsVar = path => path . match ( / % .+ % / )
215- const containsPosixVar = path => path . match ( / \$ .+ / )
211+ const containsWindowsVar = ( path ) => path . match ( / % .+ % / )
212+ const containsPosixVar = ( path ) => path . match ( / \$ .+ / )
216213
217214 // Safety checks
218215 const unsafenessConditions = [
@@ -226,7 +223,6 @@ function isSafePath(path) {
226223 return ! unsafenessConditions . some ( Boolean )
227224}
228225
229-
230226// System
231227
232228module . exports = {
0 commit comments