Comments (4)
Here's what I did in my use-case:
let outside = /* JS array of strings here */;
let formatter = (function formatter(i) { return outside[i] || i; });
let formatterstr = formatter.toString().replace("outside", JSON.stringify(outside));
parser = parser.uint8(name, {
formatter: new Function("x", "return (" + formatterstr + ")(x)"),
});
it would be equivalent of doing:
parser = parser.uint8(name, {
formatter: item => outside[item] || item
});
(if you come here from search, you may also be interested in my comment in #75)
from binary-parser.
Very interested in something for this. I'm in the process of replacing the old binary
module with this one (which rocks BTW!), but the lack of the ability to use my own functions easily makes some work very cumbersome.
from binary-parser.
Could own functions be injected perhaps? Something along new Parser( [range] )
?
from binary-parser.
I was going to raise a related issue but I won't because I think its root cause is the same as this one: binary-parser generates JS code that gets eval
ed, so that has profound implications on what you can do in an assert
or formatter
.
In my case I wasn't using external values at all. My formatter was simply (value) => value.readUIntLE(0, 3)
. But because I'm using TypeScript, the generated code referenced some external values so I got some pretty unhelpful errors like cov_1ux0jwh81w is not defined
.
I think there should be a big warning about this.
@p0358's workaround worked for me, although I really wish I didn't have to do that:
{
formatter: new Function('buf', 'return buf.readUIntLE(0, 3)'),
}
from binary-parser.
Related Issues (20)
- Choice keyString returns entire string HOT 2
- Combining choice with array. HOT 4
- Reading array - unknown size in bytes HOT 2
- Directly embed parse result of wrapped object? HOT 3
- Error parsing TLV with variable length data (array, choice) HOT 5
- [HELP] looking for string tokens inside binary file - how to do this when length is unknown? HOT 3
- How to handle a field that is present depending on an earlier field HOT 2
- Maximum call stack size exceeded if parsing a lot of bit fields
- Allow async "wrapper" callbacks HOT 3
- Array containing arrays are not getting parsed HOT 2
- Building parsers programmatically (question) HOT 2
- Difference between bit8 and uint8 HOT 1
- Parse order is wrong with bit fields followed by nest
- Array with non 8bit alignment HOT 1
- Old array of strings work around no longer works due to typing HOT 1
- Referencing target buffer in parser
- Cleanup non issue
- Feature request
- How to use it in js HOT 3
- Turning Objects Back into Buffers? HOT 2
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 binary-parser.