Comments (9)
@mvayngrib I agree that can be really useful, but it means the complexity of the modue will grow significantly to handle it.
from typeforce.
i took a whack at it, still looks pretty simple and only a couple of lines longer, let me know what you think https://github.com/mvayngrib/typeforce/tree/typeOpts
from typeforce.
@mvayngrib I'd rather not penalize every call with a need to check over the string for |
characters, even if we made use of regex this is probably way slower.
Definitely need to test that though.
If we do support this, unless the implementation is on-par, perhaps we could include it as a sub-module?
from typeforce.
@dcousens good point, I wasn't really thinking about performance. Let's leave it be for now, see if anyone else cares about this feature
from typeforce.
@mvayngrib what are your thoughts on the following style of syntax (pick and choose, I just brain dumped a few ideas here):
typeforce({
foo: typeforce.maybe("String") || typeforce.String.maybe,
bar: "Number" || typeforce.Number,
fizz: typeforce.any(["String", "Number", { b: "String" }, typeforce.value(3.14)]),
object: typeforce.instanceOf(CustomType)
}, value)
This would allow us to handle Maybe
types better than using ?
, and sum types with oneOf
or some such.
The string typing is a bit limiting (in both performance/features) at the moment and could probably be sped up with typeforce.String
or String
objects.
I'd like to keep some easily serializable types though for JSON
type schemas, so maybe the same types we have now, but with functions
are callable.
from typeforce.
@rubensayshi any thoughts?
from typeforce.
Added in eec6969
Syntax:
typeforce.oneOf(["String", "Number", { b: "String" }, typeforce.value(3.14)]),
from typeforce.
@dcousens sorry, I'm a bit slow. I realize I opened this issue, but I kind of changed my mind since then :)
I like and appreciate the flexibility. However, I now realize that it encourages developers to write their methods in a hard to use and maintain (if flexible) way. What I hate most about node's core APIs is this kind of stuff:
from dgram:
socket.bind(port[, address][, callback])
socket.bind(options[, callback])
very flexible, and ultimately a pain. I'd much rather they just support the 2nd one
These days, I'm still using typeforce and almost always in this use case:
typeforce({
msg: 'Buffer',
to: 'String',
public: '?Boolean'
}, options)
If it gets more complicated than that, I rewrite the method.
from typeforce.
Wholeheartedly agree with you.
However, that doesn't mean sum types can't still be useful.
For example, I could now specify the following:
function Buffer256bit (v) { return Buffer.isBuffer(v) && v.length == 32 }
function Buffer128bit (v) { return Buffer.isBuffer(v) && v.length == 16 }
var inputBuffer = "oops"
typeforce(typeforce.oneOf(Buffer256bit, Buffer128bit), inputBuffer)
// Expected Buffer256bit|Buffer128bit, got String "oops"
A bit contrived in this example, but I think the use case speaks for itself in that you can use typing in even stricter ways using this syntax.
from typeforce.
Related Issues (20)
- Standard failing HOT 3
- maybe/oneOf JSON when used with complex types outputs [object Object]
- Change quacksLike to check expected properties, not name HOT 12
- Avoid recursive throwing / return Error HOT 5
- propertyKeyType for `typeforce.map` HOT 1
- typeforce 1.5.1 breaks bitcoinjs-lib 1.5.8 HOT 25
- Document quacksLike / tuple / map? HOT 4
- strict in quacksLike? HOT 2
- breaks when uglified with mangle:true HOT 12
- Add uglfiyjs mangling warning to README HOT 1
- isFiniteNumber? HOT 2
- split on multiple files HOT 2
- Tuple w/ strict should enforce maximum length HOT 1
- `.Object` and `.object` are too similar HOT 3
- Remove typeforce.object in favour of typeforce.compile
- Optional error information HOT 4
- Add exception capturing tests for arrayOf, map, object and tuple
- Error.captureStackTrace is undefined HOT 3
- anyOf should accept `Array` argument, not `arguments`
- Tests failing in Node <LTS
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typeforce.