intertype
Advanced tools
Comparing version 0.101.11 to 0.102.0
@@ -138,2 +138,9 @@ (function() { | ||
//......................................................................................................... | ||
declare.cardinal({ | ||
isa: function(x) { | ||
return (Number.isInteger(x)) && (x >= 0); | ||
}, | ||
default: 0 | ||
}); | ||
//......................................................................................................... | ||
declare.zero({ | ||
@@ -140,0 +147,0 @@ isa: function(x) { |
@@ -41,2 +41,3 @@ (function() { | ||
GUY.props.hide(this, 'isa', new Proxy({}, this._get_hedge_base_proxy_cfg(this, '_isa'))); | ||
GUY.props.hide(this, 'cast', new Proxy({}, this._get_hedge_base_proxy_cfg(this, '_cast'))); | ||
GUY.props.hide(this, 'validate', new Proxy({}, this._get_hedge_base_proxy_cfg(this, '_validate'))); | ||
@@ -132,3 +133,3 @@ GUY.props.hide(this, 'create', new Proxy({}, this._get_hedge_base_proxy_cfg(this, '_create'))); | ||
/* TAINT code below never used? */ | ||
if (method_name === '_create') { | ||
if (method_name === '_create' || method_name === '_cast') { | ||
f = H.nameit(key, function(cfg = null) { | ||
@@ -387,2 +388,19 @@ return self[self.state.method](key, cfg); | ||
//--------------------------------------------------------------------------------------------------------- | ||
_cast(type, ...P) { | ||
var cast, type_dsc; | ||
cast = null; | ||
//....................................................................................................... | ||
if ((type_dsc = GUY.props.get(this.registry, type, null)) == null) { | ||
throw new E.Intertype_ETEMPTBD('^intertype.cast@11^', `unknown type ${rpr(type)}`); | ||
} | ||
if ((cast = GUY.props.get(type_dsc, 'cast', null)) == null) { | ||
throw new E.Intertype_ETEMPTBD('^intertype.cast@11^', `type ${rpr(type)} does not have a \`cast\` method`); | ||
} | ||
//....................................................................................................... | ||
/* NOTE we *could* call `create`, `validate`, but should we? */ | ||
// return ( @create[ type ] cast.call @, P... ) if GUY.props.has type.dsc, 'create' | ||
return this.validate[type](cast.call(this, ...P)); | ||
} | ||
_split_hedgerow_text(hedgerow) { | ||
@@ -412,2 +430,4 @@ return hedgerow.split(this.cfg.sep); | ||
// return cast.call @, P... | ||
//--------------------------------------------------------------------------------------------------------- | ||
@@ -414,0 +434,0 @@ Intertype.prototype.equals = H.equals; |
@@ -196,2 +196,5 @@ (function() { | ||
return H.nameit(field_dsc, function(x) { | ||
if (x == null) { | ||
return false; | ||
} | ||
return this._isa(...hedges, x[fieldname]); | ||
@@ -265,2 +268,15 @@ }); | ||
hub.state.isa_depth++; | ||
ref = this.fields; | ||
//..................................................................................................... | ||
for (_ in ref) { | ||
f = ref[_]; | ||
R = f(x); | ||
// debug '^767-3^', dsc, x | ||
hub.push_hedgeresult(['▲tw5', hub.state.isa_depth, f.name, x, R]); | ||
if ((R === false) || (R !== true)) { | ||
hub.state.isa_depth--; | ||
return R; | ||
} | ||
} | ||
//..................................................................................................... | ||
R = this.isa(x); | ||
@@ -285,15 +301,3 @@ // debug '^767-1^', dsc | ||
} | ||
ref = this.fields; | ||
//..................................................................................................... | ||
for (_ in ref) { | ||
f = ref[_]; | ||
R = f(x); | ||
// debug '^767-3^', dsc, x | ||
hub.push_hedgeresult(['▲tw5', hub.state.isa_depth, f.name, x, R]); | ||
if ((R === false) || (R !== true)) { | ||
hub.state.isa_depth--; | ||
return R; | ||
} | ||
} | ||
//..................................................................................................... | ||
hub.state.isa_depth--; | ||
@@ -300,0 +304,0 @@ return true; |
{ | ||
"name": "intertype", | ||
"version": "0.101.11", | ||
"version": "0.102.0", | ||
"description": "A JavaScript typechecker", | ||
@@ -5,0 +5,0 @@ "main": "lib/main.js", |
@@ -413,2 +413,29 @@ | ||
## Intertype `cast` | ||
Experimental feature to create a new instance of a type from any number of arguments; usage: | ||
```coffee | ||
declare.quantity | ||
fields: | ||
value: 'float' | ||
unit: 'nonempty.text' | ||
extras: false | ||
default: | ||
value: 0 | ||
unit: null | ||
cast: ( x ) -> | ||
return x unless @isa.nonempty.text x | ||
return x unless ( match = x.match /^(?<value>.*?)(?<unit>\D*)$/ )? | ||
{ value | ||
unit } = match.groups | ||
value = parseFloat value | ||
return x unless isa.float value | ||
return x unless isa.nonempty.text unit | ||
return { value, unit, } | ||
cast.quantity '102kg' # { value: 102, unit: 'kg', } | ||
``` | ||
Return value will be validated. | ||
## Intertype `create` | ||
@@ -615,2 +642,13 @@ | ||
picked up by `validate` when throwing error | ||
* **[–]** relax restriction on `extras` which currently calls for `default` (to be renamed -> `template`) to | ||
be set; `fields` should be sufficient | ||
* **[–]** declarations themselves should have `extras: false` to help catch unknown and misspelled | ||
properties | ||
* **[–]** A function call that *could* happen *before* shape-testing could be called `cast()` which | ||
might accept inputs of all kinds and shapes and try to return a conformant value from those or fail with | ||
an exception. So `quantity` could be declared as an object with fields `amount: 'float', unit: | ||
'nonempty.text'`, but also accept textual inputs like `'7.3e3kg`, to be parsed and transformed into | ||
`amount: 7300, unit: 'kg'`. Whether one wants `cast()` to be called always (implicitly) or only on demand | ||
is another question. | ||
* **[–]** phase out dollar sigil for field declarations; instead, use `fields` sub-object | ||
@@ -678,1 +716,5 @@ ## Is Done | ||
``` | ||
* **[+]** in compound data types, `isa()` should be called *after* fields have been validated so that a | ||
consumer can perform additional checking in `isa()` knowing that the general shape of the value is | ||
conformant. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
335973
1915
717