Skip to content

Instantly share code, notes, and snippets.

@char0n
Forked from anonymous/utils.js
Last active November 16, 2017 17:02
Show Gist options
  • Save char0n/1682a23c7663dc7140932c5b05128b09 to your computer and use it in GitHub Desktop.
Save char0n/1682a23c7663dc7140932c5b05128b09 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));
// 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