Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support BigInt in idToChunks, idToPath and pathToId #163

Open
tshemsedinov opened this issue Aug 9, 2018 · 3 comments
Open

Support BigInt in idToChunks, idToPath and pathToId #163

tshemsedinov opened this issue Aug 9, 2018 · 3 comments
Assignees

Comments

@tshemsedinov
Copy link
Member

  • Check if BigInt supported, use functions version that support it
  • Otherwise use current implementation

Refs: #147

@SemenchenkoVitaliy
Copy link
Member

SemenchenkoVitaliy commented Aug 10, 2018

@tshemsedinov There is no problem with actual functions but, as I said earlier, eslint currently does not support BigInt:

/home/vitaliy/common/lib/id.js
  150:12  error  A function with a name starting with an uppercase letter should only be used as a constructor  new-cap
  150:12  error  'BigInt' is not defined                                                                        no-undef

✖ 2 problems (2 errors, 0 warnings)

as well as metatests:

$ node test/all.js
/home/vitaliy/nodejs/common/node_modules/metatests/lib/case.js:73
          sResult = JSON.stringify(result);
                         ^

TypeError: Do not know how to serialize a BigInt
    at JSON.stringify (<anonymous>)
    at Object.metatests.case (/home/vitaliy/nodejs/common/node_modules/metatests/lib/case.js:73:26)
    at Object.<anonymous> (/home/vitaliy/nodejs/common/test/id.js:11:19)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)

though BigInt.prototype.toString() does

$  node
> (BigInt(1234567890)).toString()
'1234567890'

so CI tests will fail in any case.
What should I do with that?

@SemenchenkoVitaliy
Copy link
Member

SemenchenkoVitaliy commented Aug 10, 2018

Also we can temporary use

BigInt.prototype.toJSON = function(){return this.toString()+'n'}

to stringify BigInt:

$ node
> BigInt.prototype.toJSON = function(){return this.toString()+'n'}
[Function]
> console.log(JSON.stringify({a:BigInt(123)}))
{"a":"123n"}
undefined

and

JSON.parse('{"a": "123n"}', (key, value) => {
  if(typeof value === 'string' && value.lastIndexOf('n') === value.length - 1) {
    let i;
    for(i = 0; i < value.length - 1; i++) {
      if (value[i] !== '0' &&
          value[i] !== '1' &&
          value[i] !== '2' &&
          value[i] !== '3' &&
          value[i] !== '4' &&
          value[i] !== '5' &&
          value[i] !== '6' &&
          value[i] !== '7' &&
          value[i] !== '8' &&
          value[i] !== '9') return value;
    }
    return BigInt(value.slice(0, value.length - 1));
  }
  return value;
})

to parse it:

$ node
> JSON.parse('{"a": "123n"}', (key, value) => {
...   if(typeof value === 'string' && value.lastIndexOf('n') === value.length - 1) {
.....     let i;
.....     for(i = 0; i < value.length - 1; i++) {
.......       if (value[i] !== '0' &&
.........           value[i] !== '1' &&
.........           value[i] !== '2' &&
.........           value[i] !== '3' &&
.........           value[i] !== '4' &&
.........           value[i] !== '5' &&
.........           value[i] !== '6' &&
.........           value[i] !== '7' &&
.........           value[i] !== '8' &&
.........           value[i] !== '9') return value;
.......     }
.....     return BigInt(value.slice(0, value.length - 1));
.....   }
...   return value;
... })
{ a: 123n }

@tshemsedinov what do you think of that?

@SemenchenkoVitaliy
Copy link
Member

Do we still need it? And what about Uint64, which we will use in future? If we do, I think it will be better to make separate functions for them.
BTW, idToPath and idToChunks are already compatible with both of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants