Created
November 16, 2017 12:09
-
-
Save anonymous/c591a78a367dd5fa68fea7e920160818 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)); | |
// toByteRange :: Array -> Array | |
export const toByteOffset = ([begin, count]) => begin + count; | |
// toLineNumber :: String -> Number -> String -> Number | |
export const toLineNumber = 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