decoders 2.0.3 → 2.0.5

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/lib/objects.mjs CHANGED
@@ -10,7 +10,6 @@ export function object(decodersByKey) {
10
10
  var knownKeys = new Set(Object.keys(decodersByKey))
11
11
  return pojo.then(function (plainObj, ok, err) {
12
12
  var actualKeys = new Set(Object.keys(plainObj))
13
-
14
13
  var missingKeys = subtract(knownKeys, actualKeys)
15
14
  var record = {}
16
15
  var errors = null
@@ -18,37 +17,29 @@ export function object(decodersByKey) {
18
17
  var decoder = decodersByKey[key]
19
18
  var rawValue = plainObj[key]
20
19
  var result = decoder.decode(rawValue)
21
-
22
20
  if (result.ok) {
23
21
  var value = result.value
24
-
25
22
  if (value !== undefined) {
26
23
  record[key] = value
27
24
  }
28
-
29
25
  missingKeys['delete'](key)
30
26
  } else {
31
27
  var ann = result.error
32
-
33
28
  if (rawValue === undefined) {
34
29
  missingKeys.add(key)
35
30
  } else {
36
31
  if (errors === null) {
37
32
  errors = {}
38
33
  }
39
-
40
34
  errors[key] = ann
41
35
  }
42
36
  }
43
37
  })
44
-
45
38
  if (errors || missingKeys.size > 0) {
46
39
  var objAnn = annotateObject(plainObj)
47
-
48
40
  if (errors) {
49
41
  objAnn = merge(objAnn, errors)
50
42
  }
51
-
52
43
  if (missingKeys.size > 0) {
53
44
  var errMsg = Array.from(missingKeys)
54
45
  .map(function (key) {
@@ -58,23 +49,19 @@ export function object(decodersByKey) {
58
49
  var pluralized = missingKeys.size > 1 ? 'keys' : 'key'
59
50
  objAnn = updateText(objAnn, 'Missing ' + pluralized + ': ' + errMsg)
60
51
  }
61
-
62
52
  return err(objAnn)
63
53
  }
64
-
65
54
  return ok(record)
66
55
  })
67
56
  }
68
57
 
69
58
  export function exact(decodersByKey) {
70
59
  var allowedKeys = new Set(Object.keys(decodersByKey))
71
-
72
60
  var checked = pojo.reject(function (plainObj) {
73
61
  var actualKeys = new Set(Object.keys(plainObj))
74
62
  var extraKeys = subtract(actualKeys, allowedKeys)
75
63
  return extraKeys.size > 0 ? 'Unexpected extra keys: ' + Array.from(extraKeys).join(', ') : null
76
64
  })
77
-
78
65
  return checked.then(object(decodersByKey).decode)
79
66
  }
80
67
 
@@ -83,7 +70,6 @@ export function inexact(decodersByKey) {
83
70
  var allkeys = new Set(Object.keys(plainObj))
84
71
  var decoder = object(decodersByKey).transform(function (safepart) {
85
72
  var safekeys = new Set(Object.keys(decodersByKey))
86
-
87
73
  safekeys.forEach(function (k) {
88
74
  return allkeys.add(k)
89
75
  })
@@ -91,7 +77,6 @@ export function inexact(decodersByKey) {
91
77
  allkeys.forEach(function (k) {
92
78
  if (safekeys.has(k)) {
93
79
  var value = safepart[k]
94
-
95
80
  if (value !== undefined) {
96
81
  rv[k] = value
97
82
  }
@@ -112,22 +97,18 @@ export function dict(decoder) {
112
97
  Object.keys(plainObj).forEach(function (key) {
113
98
  var value = plainObj[key]
114
99
  var result = decoder.decode(value)
115
-
116
100
  if (result.ok) {
117
101
  if (errors === null) {
118
102
  rv[key] = result.value
119
103
  }
120
104
  } else {
121
105
  rv = {}
122
-
123
106
  if (errors === null) {
124
107
  errors = {}
125
108
  }
126
-
127
109
  errors[key] = result.error
128
110
  }
129
111
  })
130
-
131
112
  if (errors !== null) {
132
113
  return err(merge(annotateObject(plainObj), errors))
133
114
  } else {
package/lib/strings.js CHANGED
@@ -4,14 +4,11 @@ exports.__esModule = true
4
4
  exports.nonEmptyString = exports.httpsUrl = exports.email = void 0
5
5
  exports.regex = regex
6
6
  exports.uuidv4 = exports.uuidv1 = exports.uuid = exports.url = exports.string = void 0
7
-
8
7
  var _Decoder = require('../Decoder')
9
-
10
8
  var _unions = require('./unions')
11
-
12
9
  var _utilities = require('./utilities')
13
10
 
14
- var url_re = /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,\w]*)?(?:#[.,!/\w]*)?)?$/
11
+ var url_re = /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,/\w]*)?(?:#[.,!/\w]*)?)?$/
15
12
 
16
13
  var string = (0, _Decoder.define)(function (blob, ok, err) {
17
14
  return typeof blob === 'string' ? ok(blob) : err('Must be string')
@@ -21,7 +18,6 @@ exports.string = string
21
18
  var nonEmptyString = regex(/\S/, 'Must be non-empty string')
22
19
 
23
20
  exports.nonEmptyString = nonEmptyString
24
-
25
21
  function regex(regex, msg) {
26
22
  return string.refine(function (s) {
27
23
  return regex.test(s)
@@ -13,7 +13,7 @@ import type { Decoder } from '../Decoder';
13
13
  * \5 - the path (optional)
14
14
  */
15
15
  const url_re =
16
- /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,\w]*)?(?:#[.,!/\w]*)?)?$/;
16
+ /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,/\w]*)?(?:#[.,!/\w]*)?)?$/;
17
17
 
18
18
  /**
19
19
  * Accepts and returns strings.
package/lib/strings.mjs CHANGED
@@ -2,7 +2,7 @@ import { define } from '../Decoder.mjs'
2
2
  import { either } from './unions.mjs'
3
3
  import { instanceOf } from './utilities.mjs'
4
4
 
5
- var url_re = /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,\w]*)?(?:#[.,!/\w]*)?)?$/
5
+ var url_re = /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,/\w]*)?(?:#[.,!/\w]*)?)?$/
6
6
 
7
7
  export var string = define(function (blob, ok, err) {
8
8
  return typeof blob === 'string' ? ok(blob) : err('Must be string')
package/lib/unions.js CHANGED
@@ -4,15 +4,10 @@ exports.__esModule = true
4
4
  exports.either = void 0
5
5
  exports.oneOf = oneOf
6
6
  exports.taggedUnion = taggedUnion
7
-
8
7
  var _Decoder = require('../Decoder')
9
-
10
8
  var _utils = require('../_utils')
11
-
12
9
  var _objects = require('./objects')
13
-
14
10
  var _utilities = require('./utilities')
15
-
16
11
  var EITHER_PREFIX = 'Either:\n'
17
12
 
18
13
  function itemize(s) {
@@ -27,24 +22,19 @@ function _either() {
27
22
  for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
28
23
  decoders[_key] = arguments[_key]
29
24
  }
30
-
31
25
  if (decoders.length === 0) {
32
26
  throw new Error('Pass at least one decoder to either()')
33
27
  }
34
-
35
28
  return (0, _Decoder.define)(function (blob, _, err) {
36
29
  var errors = []
37
-
38
30
  for (var _i = 0; _i < decoders.length; _i++) {
39
31
  var result = decoders[_i].decode(blob)
40
-
41
32
  if (result.ok) {
42
33
  return result
43
34
  } else {
44
35
  errors.push(result.error)
45
36
  }
46
37
  }
47
-
48
38
  var text =
49
39
  EITHER_PREFIX +
50
40
  errors
@@ -59,17 +49,14 @@ function _either() {
59
49
  var either = _either
60
50
 
61
51
  exports.either = either
62
-
63
52
  function oneOf(constants) {
64
53
  return (0, _Decoder.define)(function (blob, ok, err) {
65
54
  var winner = constants.find(function (c) {
66
55
  return c === blob
67
56
  })
68
-
69
57
  if (winner !== undefined) {
70
58
  return ok(winner)
71
59
  }
72
-
73
60
  return err(
74
61
  'Must be one of ' +
75
62
  constants
@@ -83,7 +70,6 @@ function oneOf(constants) {
83
70
 
84
71
  function taggedUnion(field, mapping) {
85
72
  var _object
86
-
87
73
  var base = (0, _objects.object)(((_object = {}), (_object[field] = (0, _utilities.prep)(String, oneOf(Object.keys(mapping)))), _object)).transform(function (o) {
88
74
  return o[field]
89
75
  })
package/lib/unions.mjs CHANGED
@@ -16,24 +16,19 @@ function _either() {
16
16
  for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
17
17
  decoders[_key] = arguments[_key]
18
18
  }
19
-
20
19
  if (decoders.length === 0) {
21
20
  throw new Error('Pass at least one decoder to either()')
22
21
  }
23
-
24
22
  return define(function (blob, _, err) {
25
23
  var errors = []
26
-
27
24
  for (var _i = 0; _i < decoders.length; _i++) {
28
25
  var result = decoders[_i].decode(blob)
29
-
30
26
  if (result.ok) {
31
27
  return result
32
28
  } else {
33
29
  errors.push(result.error)
34
30
  }
35
31
  }
36
-
37
32
  var text =
38
33
  EITHER_PREFIX +
39
34
  errors
@@ -52,11 +47,9 @@ export function oneOf(constants) {
52
47
  var winner = constants.find(function (c) {
53
48
  return c === blob
54
49
  })
55
-
56
50
  if (winner !== undefined) {
57
51
  return ok(winner)
58
52
  }
59
-
60
53
  return err(
61
54
  'Must be one of ' +
62
55
  constants
@@ -70,7 +63,6 @@ export function oneOf(constants) {
70
63
 
71
64
  export function taggedUnion(field, mapping) {
72
65
  var _object
73
-
74
66
  var base = object(((_object = {}), (_object[field] = prep(String, oneOf(Object.keys(mapping)))), _object)).transform(function (o) {
75
67
  return o[field]
76
68
  })
package/lib/utilities.js CHANGED
@@ -6,9 +6,7 @@ exports.instanceOf = instanceOf
6
6
  exports.lazy = lazy
7
7
  exports.never = never
8
8
  exports.prep = prep
9
-
10
9
  var _annotate = require('../annotate')
11
-
12
10
  var _Decoder = require('../Decoder')
13
11
 
14
12
  function instanceOf(klass) {
@@ -26,13 +24,11 @@ function lazy(decoderFn) {
26
24
  function prep(mapperFn, decoder) {
27
25
  return (0, _Decoder.define)(function (originalInput, _, err) {
28
26
  var blob
29
-
30
27
  try {
31
28
  blob = mapperFn(originalInput)
32
29
  } catch (e) {
33
30
  return err((0, _annotate.annotate)(originalInput, e.message))
34
31
  }
35
-
36
32
  var r = decoder.decode(blob)
37
33
  return r.ok ? r : err((0, _annotate.annotate)(originalInput, r.error.text))
38
34
  })
package/lib/utilities.mjs CHANGED
@@ -16,13 +16,11 @@ export function lazy(decoderFn) {
16
16
  export function prep(mapperFn, decoder) {
17
17
  return define(function (originalInput, _, err) {
18
18
  var blob
19
-
20
19
  try {
21
20
  blob = mapperFn(originalInput)
22
21
  } catch (e) {
23
22
  return err(annotate(originalInput, e.message))
24
23
  }
25
-
26
24
  var r = decoder.decode(blob)
27
25
  return r.ok ? r : err(annotate(originalInput, r.error.text))
28
26
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "decoders",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "Elegant and battle-tested validation library for type-safe input data (for TypeScript and Flow)",
5
5
  "license": "MIT",
6
6
  "repository": {