Sha256: 02a84f9fd9e7394171ce58e746c74045310bc4edf8505e46fdf5d61e9045e89d

Contents?: true

Size: 1.21 KB

Versions: 19

Compression:

Stored size: 1.21 KB

Contents

/*!
 * align-text <https://github.com/jonschlinkert/align-text>
 *
 * Copyright (c) 2015, Jon Schlinkert.
 * Licensed under the MIT License.
 */

'use strict';

var typeOf = require('kind-of');
var repeat = require('repeat-string');
var longest = require('longest');

module.exports = function alignText(val, fn) {
  var lines, type = typeOf(val);

  if (type === 'array') {
    lines = val;
  } else if (type === 'string') {
    lines = val.split(/(?:\r\n|\n)/);
  } else {
    throw new TypeError('align-text expects a string or array.');
  }

  var fnType = typeOf(fn);
  var len = lines.length;
  var max = longest(lines);
  var res = [], i = 0;

  while (len--) {
    var line = String(lines[i++]);
    var diff;

    if (fnType === 'function') {
      diff = fn(line.length, max.length, line, lines, i);
    } else if (fnType === 'number') {
      diff = fn;
    } else {
      diff = max.length - line.length;
    }

    if (typeOf(diff) === 'number') {
      res.push(repeat(' ', diff) + line);
    } else if (typeOf(diff) === 'object') {
      var result = repeat(diff.character || ' ', diff.indent || 0);
      res.push((diff.prefix || '') + result + line);
    }
  }

  if (type === 'array') return res;
  return res.join('\n');
};

Version data entries

19 entries across 16 versions & 8 rubygems

Version Path
ilog-0.4.1 node_modules/align-text/index.js
ilog-0.4.0 node_modules/align-text/index.js
ilog-0.3.3 node_modules/align-text/index.js
optimacms-0.4.3 spec/dummy/node_modules/align-text/index.js
optimacms-0.4.2 spec/dummy/node_modules/align-text/index.js
locomotivecms-3.4.0 app/javascript/node_modules/align-text/index.js
cortex-0.1.3 spec/dummy/node_modules/align-text/index.js
lanes-0.8.0 node_modules/align-text/index.js
xcodebuild-helper-1.2.5 externals/ios-sim-master/node_modules/align-text/index.js
xcodebuild-helper-1.2.3 externals/ios-sim-master/node_modules/align-text/index.js
es6_tilt-0.1.2 test/dummy/app/assets/javascripts/node_modules/align-text/index.js
es6_tilt-0.1.1 test/dummy/app/assets/javascripts/node_modules/align-text/index.js
es6_tilt-0.1.0 test/dummy/app/assets/javascripts/node_modules/align-text/index.js
gulp_assets-1.0.0.pre.5 template/node_modules/gulp-sass/node_modules/node-sass/node_modules/sass-graph/node_modules/yargs/node_modules/cliui/node_modules/right-align/node_modules/align-text/index.js
gulp_assets-1.0.0.pre.5 template/node_modules/gulp-sass/node_modules/node-sass/node_modules/sass-graph/node_modules/yargs/node_modules/cliui/node_modules/center-align/node_modules/align-text/index.js
gulp_assets-1.0.0.pre.4 template/node_modules/gulp-sass/node_modules/node-sass/node_modules/sass-graph/node_modules/yargs/node_modules/cliui/node_modules/right-align/node_modules/align-text/index.js
gulp_assets-1.0.0.pre.4 template/node_modules/gulp-sass/node_modules/node-sass/node_modules/sass-graph/node_modules/yargs/node_modules/cliui/node_modules/center-align/node_modules/align-text/index.js
gulp_assets-1.0.0.pre.3 template/node_modules/gulp-sass/node_modules/node-sass/node_modules/sass-graph/node_modules/yargs/node_modules/cliui/node_modules/center-align/node_modules/align-text/index.js
gulp_assets-1.0.0.pre.3 template/node_modules/gulp-sass/node_modules/node-sass/node_modules/sass-graph/node_modules/yargs/node_modules/cliui/node_modules/right-align/node_modules/align-text/index.js