-
-
Save char0n/1682a23c7663dc7140932c5b05128b09 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { curry, defaultTo, length } from 'ramda'; | |
import { Buffer } from 'buffer'; // Buffer is global in node environment | |
// stringToBuffer :: String -> String -> Uint8Array | |
export const stringToBuffer = curry((encoding, string) => Buffer.from(string, encoding)); | |
// byteRange :: Array -> Array | |
export const byteOffset = ([begin]) => begin; | |
// lineNumber :: String -> Number -> String -> Number | |
export const lineNumber = curry((encoding, offset, input) => { | |
const buffer = stringToBuffer(encoding, input); | |
const stringFragment = buffer.toString(encoding, 0, offset); | |
const matches = defaultTo([], stringFragment.match(/\n/gm)); | |
return length(matches) + 1; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment