Skip to content

Instantly share code, notes, and snippets.

Created November 16, 2017 12:09
Show Gist options
  • Save anonymous/c591a78a367dd5fa68fea7e920160818 to your computer and use it in GitHub Desktop.
Save anonymous/c591a78a367dd5fa68fea7e920160818 to your computer and use it in GitHub Desktop.
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