nzcp-js icon indicating copy to clipboard operation
nzcp-js copied to clipboard

Get the library to work in React Native

Open noway opened this issue 4 years ago • 5 comments
trafficstars

Tests should pass in React Native environment

noway avatar Nov 06 '21 03:11 noway

Polyfilling TextEncoding (seems to) work currently, stuck on finding a suitable implementation of BigInt currently (that supports 0x... notation). I'm not sure what other core libraries don't have inbuilt React Native equivalents.

Also (attempting to) ping @meshcollider

NeedsAdjustment avatar Nov 17 '21 20:11 NeedsAdjustment

Successfully got polyfills working for a react native implementation (soon to be public), based on modified code from this. Imo using a helper package is a bit clunky, but it's a possible way forward.

NeedsAdjustment avatar Nov 17 '21 22:11 NeedsAdjustment

Specifically:

import 'text-encoding-polyfill'
global.Buffer = global.Buffer || require('buffer').Buffer

if (typeof BigInt === 'undefined') {
    const bi = require('big-integer')

    function myBigInt(value) {
        if (typeof value === 'string') {
            const match = value.match(/^0([xo])([0-9a-f]+)$/i)
            if (match) {
                return bi(match[2], match[1].toLowerCase() === 'x' ? 16 : 8)
            }
        }
        return bi(value)
    }

    global.BigInt = myBigInt

    if (process === undefined) {
        process = require('process')
    } else if (process.nextTick === undefined) {
        process.nextTick = require('process').nextTick
    }
}

NeedsAdjustment avatar Nov 18 '21 12:11 NeedsAdjustment

We have two React Native apps now using this package, I suggest this closes.

rafcontreras avatar Nov 22 '21 09:11 rafcontreras

Let's write down some documentation, maybe create REACT_NATIVE.md and link it in README.md?

noway avatar Nov 22 '21 10:11 noway