Giter Club home page Giter Club logo

epc-tds's Issues

Invalid results with values larger than MAX_SAFE_INTEGER

Hi,

Thanks for the library, very useful.

After starting to experiment with this library, I ran into problems which seem to originate from how BigInts and Numbers are being handled. I played with GIAI-96 values, but I suspect this issue might apply to many more formats as well. Case example:

// Testing with 34144B5A1C6A94D74F834DC7 / urn:epc:tag:giai-96:0.1234567.30000000004214215

const t = tds.fromTagURI("urn:epc:tag:giai-96:0.1234567.30000000004214215")
console.log(t.toTagURI())  // 'urn:epc:tag:giai-96:0.1234567.30000000004214216'   <-- incorrect assetReference
console.log(t.getAssetReference()) // 30000000004214216    <-- incorrect assetReference
console.log(t.toHexString())  // '34144B5A1C6A94D74F834DC8'    <-- incorrect hexString

As far as I could investigate this, this seems to relate to the fact that 30000000004214215 > Number.MAX_SAFE_INTEGER. In https://github.com/sergiss/epc-tds/blob/master/epc/utils/bit-array.js#L78 a BigInt is casted to a Number, where things fail:

> var big = BigInt(30000000004214215)
undefined
> Number(big)
30000000004214216

I'm afraid I don't understand all the corners of the library and EPC values well-enough to provide a proper fix, but I believe BigInts should be used everywhere in the library when dealing with the values. Then, when outputting the values, perhaps a bigger question from dev experience point of view is whether e.g. getAssetReference() should output BigInts or strings - I guess BigInt would be more correct.

Here's a simple snippet which could be added to your test.js for validating this case with GIAI-96 values, might be useful:

function withLargeValues() {
    // 30000000004214215 > Number.MAX_SAFE_INTEGER
    const tagUri = "urn:epc:tag:giai-96:0.1234567.30000000004214215"
    const epc = tds.fromTagURI(tagUri)
    const resultTagUri = epc.toTagURI()

    if (resultTagUri !== tagUri) {
        throw Error(`Giai96, expected: ${tagUri}, got: ${resultTagUri}`)
    }
}
withLargeValues()

EAN8 to EPC and EPC to EAN8 produces different results

As far as I understand if I encode EAN8 value to EPC and then decode this EPC back to EAN I should get the original EAN value but this never happens.

Example of code:

`
const epcToEan = (epc: string): string => {
const data = tds.valueOf(epc);

const result = data.toBarcode() as string;

return result;
};

const eanToEpc = (ean: string): string => {
const epc = new tds.Sgtin96().setGtin(ean);

return epc.toHexString();
};

console.log('eanToEpc -> epcToEan:', epcToEan(eanToEpc('60573421')));
`

I was expecting to receive 60573421 as a result but I receive 60000005734216.

What am I doing wrong?

Typescript support

Hi,
First of all many thanks for this library.
May I know whether this library supports typescript ?

Browser support

Hello,

Is it possible to use this library in a web browser?

Unsupported EPC

Apparently I have an unsupported EPC, I'm not familiar with the common ones, is this not a common EPC?

4453490063370A451349BB6E

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.