yggio 0.0.14 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/fields.js CHANGED
@@ -1,49 +1,45 @@
1
1
  'use strict'
2
2
 
3
- function metadataEntry(key, value) {
4
- if (typeof key !== 'string' || typeof value !== 'string') {
5
- throw new TypeError('field metadata keys and values must be strings')
6
- }
7
- return { key, value }
8
- }
3
+ const { isPlainObject } = require('./properties.js')
9
4
 
10
- function normalizeMetadata(values) {
11
- if (values === undefined) return undefined
5
+ // `values` as `{ [keyName], [valueName] }` entries: a plain object, an array
6
+ // of pairs or of entry objects, or a Map or any other iterable of pairs - the
7
+ // ways JavaScript spells one set of named strings. `label` names the set in
8
+ // a refusal.
9
+ function pairEntries(values, keyName, valueName, label) {
10
+ const entry = (key, value) => {
11
+ if (typeof key !== 'string' || typeof value !== 'string') {
12
+ throw new TypeError(`${label} ${keyName}s and ${valueName}s must be strings`)
13
+ }
14
+ return { [keyName]: key, [valueName]: value }
15
+ }
16
+ const pair = (item) => {
17
+ if (!Array.isArray(item) || item.length !== 2) {
18
+ throw new TypeError(`${label} tuples must contain two items`)
19
+ }
20
+ return entry(item[0], item[1])
21
+ }
12
22
  if (Array.isArray(values)) {
13
- return values.map((entry) => {
14
- if (Array.isArray(entry)) {
15
- if (entry.length !== 2) {
16
- throw new TypeError('field metadata tuples must contain two items')
17
- }
18
- return metadataEntry(entry[0], entry[1])
19
- }
20
- if (entry === null || typeof entry !== 'object') {
21
- throw new TypeError('field metadata entries must be objects or tuples')
23
+ return values.map((item) => {
24
+ if (Array.isArray(item)) return pair(item)
25
+ if (item === null || typeof item !== 'object') {
26
+ throw new TypeError(`${label} entries must be objects or tuples`)
22
27
  }
23
- return metadataEntry(entry.key, entry.value)
28
+ return entry(item[keyName], item[valueName])
24
29
  })
25
30
  }
26
- if (isOptions(values)) {
27
- return Object.entries(values).map(([key, value]) => metadataEntry(key, value))
31
+ if (isPlainObject(values)) {
32
+ return Object.entries(values).map(([key, value]) => entry(key, value))
28
33
  }
29
- // A Map, a Field, or any other iterable of [key, value] pairs.
30
34
  if (values !== null && typeof values === 'object' && Symbol.iterator in values) {
31
- return Array.from(values, (entry) => {
32
- if (!Array.isArray(entry) || entry.length !== 2) {
33
- throw new TypeError('field metadata tuples must contain two items')
34
- }
35
- return metadataEntry(entry[0], entry[1])
36
- })
35
+ return Array.from(values, pair)
37
36
  }
38
- throw new TypeError('field metadata must be an object, Map, or entry array')
37
+ throw new TypeError(`${label} must be an object, Map, or entry array`)
39
38
  }
40
39
 
41
- function isOptions(value) {
42
- if (value === null || typeof value !== 'object' || Array.isArray(value)) {
43
- return false
44
- }
45
- const prototype = Object.getPrototypeOf(value)
46
- return prototype === Object.prototype || prototype === null
40
+ function normalizeMetadata(values) {
41
+ if (values === undefined) return undefined
42
+ return pairEntries(values, 'key', 'value', 'field metadata')
47
43
  }
48
44
 
49
45
  // Datatype-specific factories are a language-level typing convenience. They
@@ -68,7 +64,7 @@ function createFields(DataType, Field, native) {
68
64
  // different default value in each language.
69
65
  function options(value) {
70
66
  if (value === undefined) return { nullable: true, metadata: undefined }
71
- if (!isOptions(value)) {
67
+ if (!isPlainObject(value)) {
72
68
  throw new TypeError('field options must be a plain object')
73
69
  }
74
70
  const nullable = value.nullable === undefined ? true : value.nullable
@@ -84,7 +80,7 @@ function createFields(DataType, Field, native) {
84
80
  }
85
81
 
86
82
  function temporalField(kind, fallback, name, unit, value) {
87
- if (isOptions(unit)) {
83
+ if (isPlainObject(unit)) {
88
84
  value = unit
89
85
  unit = fallback
90
86
  }
@@ -92,7 +88,7 @@ function createFields(DataType, Field, native) {
92
88
  }
93
89
 
94
90
  function decimalField(kind, name, precision, scale, value) {
95
- if (isOptions(scale)) {
91
+ if (isPlainObject(scale)) {
96
92
  value = scale
97
93
  scale = 0
98
94
  }
@@ -120,7 +116,7 @@ function createFields(DataType, Field, native) {
120
116
  function parameterized(build, keys) {
121
117
  return (name, value) => {
122
118
  if (value === undefined) return field(name, build(undefined), undefined)
123
- if (!isOptions(value)) {
119
+ if (!isPlainObject(value)) {
124
120
  throw new TypeError('field options must be a plain object')
125
121
  }
126
122
  const parameters = {}
@@ -150,7 +146,7 @@ function createFields(DataType, Field, native) {
150
146
 
151
147
  datetime64(name, unit = 'microsecond', timezone = 'NAIVE', value) {
152
148
  // Omitting timezone makes the third argument the options object.
153
- if (isOptions(timezone)) {
149
+ if (isPlainObject(timezone)) {
154
150
  value = timezone
155
151
  timezone = 'NAIVE'
156
152
  }
@@ -279,7 +275,7 @@ function createFields(DataType, Field, native) {
279
275
  return field(name, DataType.fromFields(children), value)
280
276
  },
281
277
  union(name, members, mode = 'sparse', value) {
282
- if (isOptions(mode)) {
278
+ if (isPlainObject(mode)) {
283
279
  value = mode
284
280
  mode = 'sparse'
285
281
  }
@@ -301,18 +297,18 @@ function createFields(DataType, Field, native) {
301
297
  return field(name, DataType.variant(), value)
302
298
  },
303
299
  geometry(name, crs, value) {
304
- if (isOptions(crs)) {
300
+ if (isPlainObject(crs)) {
305
301
  value = crs
306
302
  crs = undefined
307
303
  }
308
304
  return field(name, DataType.geometry(crs), value)
309
305
  },
310
306
  geography(name, crs, algorithm, value) {
311
- if (isOptions(crs)) {
307
+ if (isPlainObject(crs)) {
312
308
  value = crs
313
309
  crs = undefined
314
310
  algorithm = undefined
315
- } else if (isOptions(algorithm)) {
311
+ } else if (isPlainObject(algorithm)) {
316
312
  value = algorithm
317
313
  algorithm = undefined
318
314
  }
@@ -331,7 +327,7 @@ function createFields(DataType, Field, native) {
331
327
  // nothing the fixed leaf could take, so it is refused as Python refuses
332
328
  // it, and options stated in both places are two readings of one call.
333
329
  decimal(name, precision, scale, value) {
334
- if (precision === undefined || isOptions(precision)) {
330
+ if (precision === undefined || isPlainObject(precision)) {
335
331
  if (scale !== undefined) {
336
332
  throw new TypeError('decimal(): a scale needs a precision')
337
333
  }
@@ -357,14 +353,14 @@ function createFields(DataType, Field, native) {
357
353
  },
358
354
 
359
355
  map(name, entries, keysSorted = false, value) {
360
- if (isOptions(keysSorted)) {
356
+ if (isPlainObject(keysSorted)) {
361
357
  value = keysSorted
362
358
  keysSorted = false
363
359
  }
364
360
  return field(name, native.map(entries, keysSorted), value)
365
361
  },
366
362
  mapOf(name, key, mappedValue, keysSorted = false, value) {
367
- if (isOptions(keysSorted)) {
363
+ if (isPlainObject(keysSorted)) {
368
364
  value = keysSorted
369
365
  keysSorted = false
370
366
  }
@@ -382,4 +378,4 @@ function createFields(DataType, Field, native) {
382
378
  return Object.freeze(fields)
383
379
  }
384
380
 
385
- module.exports = { createFields, normalizeMetadata }
381
+ module.exports = { createFields, normalizeMetadata, pairEntries }