How to use the detect-newline.graceful function in detect-newline

To help you get started, we’ve selected a few detect-newline examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github renke / import-sort / packages / import-sort / src / index.ts View on Github external
file?: string,
  options?: any,
): ISortResult {
  const items = addFallback(style, file, options || {})(StyleAPI);

  const buckets: Array> = items.map(() => []);

  const imports = parser.parseImports(code, {
    file,
  });

  if (imports.length === 0) {
    return {code, changes: []};
  }

  const eol = detectNewline.graceful(code);

  const changes: Array = [];

  // Fill buckets
  for (const imported of imports) {
    let sortedImport = imported;

    const index = items.findIndex(item => {
      sortedImport = sortNamedMembers(imported, item.sortNamedMembers);
      return !!item.match && item.match!(sortedImport);
    });

    if (index !== -1) {
      buckets[index].push(sortedImport);
    }
  }
github gulp-sourcemaps / gulp-sourcemaps / src / utils.js View on Github external
function getCommentFormatter(file) {
  var extension = file.relative.split('.').pop();
  var fileContents = file.contents.toString();
  var newline = detectNewline.graceful(fileContents || '');

  var commentFormatter = commentFormatters.default;

  if (file.sourceMap.preExistingComment) {
    commentFormatter = (commentFormatters[extension] || commentFormatter).bind(undefined, '', newline);
    debug(function() {
      return 'preExistingComment commentFormatter ' + commentFormatter.name;
    });
  } else {
    commentFormatter = (commentFormatters[extension] || commentFormatter).bind(undefined, newline, newline);
  }

  debug(function() {
    return 'commentFormatter ' + commentFormatter.name;
  });
  return commentFormatter;
github mlewand / win-clipboard / lib / html.js View on Github external
encode( str, sourceUrl ) {
		const eol = detectNewline.graceful( str );

		let headers = [
			'Version:0.9',
			'StartHTML:00000000',
			'EndHTML:00000000',
			'StartFragment:00000000',
			'EndFragment:00000000'
		];

		if ( sourceUrl ) {
			headers.push( 'SourceURL:' + sourceUrl );
		}

		str = this._calculateHeaders( headers, str, eol );

		headers.push( str );
github keithamus / sort-package-json / index.js View on Github external
function editStringJSON(json, over) {
  if (typeof json === 'string') {
    const { indent } = detectIndent(json)
    const endCharacters = json.slice(-1) === '\n' ? '\n' : ''
    const newline = detectNewline(json)
    json = JSON.parse(json)

    let result = JSON.stringify(over(json), null, indent) + endCharacters
    if (newline === '\r\n') {
      result = result.replace(/\n/g, newline)
    }
    return result
  }

  return over(json)
}
github massick / gulp-strip-code / index.js View on Github external
if (options == null) {
            options = {};
        };

        options.start_comment = options.start_comment == null ? 'test-code' : options.start_comment;
        options.end_comment = options.end_comment == null ? 'end-test-code' : options.end_comment;
        options.keep_comments = options.keep_comments == null ? false : options.keep_comments;
        options.comment_all = options.comment_all == null ? false : options.comment_all;

        if (options.comment_all){
            var pattern = options.pattern || new RegExp("([\\t ]*\\/\\* ?" + options.start_comment + ")[\\s\\S]*?(" + options.end_comment + " ?\\*\\/[\\t ]*\\r?\\n?)", "g");
        } else{
            var pattern = options.pattern || new RegExp("([\\t ]*\\/\\* ?" + options.start_comment + " ?\\*\\/)[\\s\\S]*?(\\/\\* ?" + options.end_comment + " ?\\*\\/[\\t ]*\\r?\\n?)", "g");
        }

        var eol = detectNewline.graceful(String(file.contents));

        if (isBuffer) {
            if(options.keep_comments){
                file.contents = Buffer.from(String(file.contents).replace(pattern, "$1" + eol + "$2"));
            }
            else{
                file.contents = Buffer.from(String(file.contents).replace(pattern, ""));
            }
            return callback(null, file);
        }

        callback(null, file);
    };

detect-newline

Detect the dominant newline character of a string

MIT
Latest version published 1 year ago

Package Health Score

70 / 100
Full package analysis

Popular detect-newline functions