javascript - Detecting type of line breaks -


what efficient (fast , reliable enough) way in javascript determine type of line breaks used in text - unix vs windows.

in node app have read in large utf-8 text files , process them based on whether use unix or windows line breaks.

when type of line breaks comes uncertain, want conclude based on 1 then.

you want first lf. source.indexof('\n') , see if character behind cr source[source.indexof('\n')-1] === '\r'. way, find first example of newline , match it. in summary,

function whichlineending(source) { var temp = source.indexof('\n'); if (source[temp - 1] === '\r') return 'crlf' return 'lf' }

there 2 popularish examples of libraries doing in npm modules: https://github.com/danielchatfield/node-newline/blob/master/lib/detect.js , https://github.com/neoklosch/crlf-helper/blob/master/lib/crlfhelper.js first split on entire string inefficient in case. second uses regex in case not quick enough.

however, edit, if want determine more plentiful. use code https://github.com/danielchatfield/node-newline/blob/master/lib/detect.js handle case.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -