Coder Social home page Coder Social logo

pad-left's Introduction

pad-left NPM version NPM downloads Build Status

Left pad a string with zeros or a specified string. Fastest implementation.

You might also be interested in word-wrap.

Install

Install with npm:

$ npm install pad-left --save

Usage

var pad = require('pad-left');
pad(  '4', 4, '0') // 0004
pad( '35', 4, '0') // 0035
pad('459', 4, '0') // 0459

Benchmarks

Benchmarks for node.js v6.5.0 versus left-pad.

# benchmark/fixtures/10-custom-char.js (37 bytes)
  pad-left x 14,660,838 ops/sec ±1.43% (84 runs sampled)
  left-pad x 9,189,792 ops/sec ±1.36% (81 runs sampled)

# benchmark/fixtures/10.js (32 bytes)
  pad-left x 13,357,570 ops/sec ±1.91% (84 runs sampled)
  left-pad x 20,375,277 ops/sec ±1.40% (82 runs sampled)

# benchmark/fixtures/100-custom-char.js (38 bytes)
  pad-left x 13,671,301 ops/sec ±1.58% (83 runs sampled)
  left-pad x 8,917,004 ops/sec ±1.84% (79 runs sampled)

# benchmark/fixtures/100.js (34 bytes)
  pad-left x 15,442,944 ops/sec ±1.68% (81 runs sampled)
  left-pad x 7,850,636 ops/sec ±1.47% (83 runs sampled)

# benchmark/fixtures/1000-custom-char.js (40 bytes)
  pad-left x 13,624,072 ops/sec ±1.25% (84 runs sampled)
  left-pad x 5,907,915 ops/sec ±1.13% (86 runs sampled)

# benchmark/fixtures/1000.js (35 bytes)
  pad-left x 14,241,788 ops/sec ±1.23% (81 runs sampled)
  left-pad x 5,418,351 ops/sec ±1.52% (80 runs sampled)

Related projects

You might also be interested in these projects:

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

Generate readme and API documentation with verb:

$ npm install verb && npm run docs

Or, if verb is installed globally:

$ verb

Running tests

Install dev dependencies:

$ npm install -d && npm test

Author

Jon Schlinkert

License

Copyright © 2016, Jon Schlinkert. Released under the MIT license.


This file was generated by verb, v0.9.0, on May 07, 2016.

pad-left's People

Contributors

cht8687 avatar jonschlinkert avatar linusu avatar mathiasvr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pad-left's Issues

To fulfill both backward-compatibility and improvement for modern engines.

ES6 has evolved a lot so far; now it has String.prototype.padStart and String.prototype.repeat.
However, pad-left is still a great module for backward-compatibility.
But since it does not use the built-in methods, modern engines may not perform their best when they run this module.
So my proposal is to let them use what they know.

To fulfill both backward-compatibility and improvement for modern engines, it could be modified like below.
In this modification, I did not make any slowdown in the function code but in the module initialization.
i.e. I did not put the if-else thing in the padLeft function body.

Original

'use strict';

var repeat = require('repeat-string');

module.exports = function padLeft(str, num, ch) {
  str = str.toString();

  if (typeof num === 'undefined') {
    return str;
  }

  if (ch === 0) {
    ch = '0';
  } else if (ch) {
    ch = ch.toString();
  } else {
    ch = ' ';
  }

  return repeat(ch, num - str.length) + str;
};

Modified

'use strict';

if("padStart" in String.prototype)
{
  module.exports = function padLeft(str, num, ch) {
    return str.toString().padStart(num, ch);
  };
}
else if("repeat" in String.prototype)
{ // Engines support `repeat` may not support `padStart`, according to http://node.green.
  module.exports = function padLeft(str, num, ch) {
    str = str.toString();

    if (typeof num === 'undefined') {
      return str;
    }

    if (ch === 0) {
      ch = '0';
    } else if (ch) {
      ch = ch.toString();
    } else {
      ch = ' ';
    }

    return ch.repeat(num - str.length) + str;
    // To avoid an extra function call, I did not wrap `String.prototype.repeat` but use it directly.
  };
}
else
{
  var repeat = require('repeat-string');
  module.exports = function padLeft(str, num, ch) {
    str = str.toString();

    if (typeof num === 'undefined') {
      return str;
    }

    if (ch === 0) {
      ch = '0';
    } else if (ch) {
      ch = ch.toString();
    } else {
      ch = ' ';
    }

    return repeat(ch, num - str.length) + str;
  };
}

To check the method support, I used in operator, based on the result from https://perf.zone.

image


I was wondering if you could let me Pull-request this one. Thank you.

Update the benchmarks

Hey, I just noticed this package, care to update the benchmarks to run against the latest left-pad?

coerce first argument to string

I think that a common use case for this module is to zero-pad or right-align numbers. Therefor I would like to propose the great addition of coercing the first argument into a string when it is a number.

Currently the following doesn't work, it would be great if it would.

padLeft(47, 3, '0')

Question about the benchmarks

I was looking at the code for pad-left and left-pad and was wondering about how fair the benchmarks are.

Both pad-left and left-pad use the same sort of algorithm for creating the padding yet in the benchmarks pad-left is about twice as fast. The reason for this difference in speed is the repeat-string package. As long as the padding char remains the same it doesn't recreate the string, it simply returns a cached version.

It is a very nice optimisation which can make quite the difference but it also skews the benchmark results. The left-pad implementation is actually padding the string on each iteration where pad-left already has part of the answer ready after the first iteration.

When benchmarking the speed of both packages, would it not be more fair if it was done without the caching of repeat-string? Or perhaps make sure no two iterations use the same padding character.

I feel that now the benchmarks are only running a single case which heavily favours the implementation of pad-left. By adding cases which would render the caching by repeat-string moot you would get a better representation of how fast both algorithms are.

Benchmars

In Benchmarks section, I see you point to the same repo.
should we update it?

Very strange usage

This library always appends a static number of characters in front of the string. I think that the expected behaviour is to pad the string so that it's always at least a specific length.

This is how every other string padding library works.

Current behaviour:

padLeft(  '4', 4, '0') // 00004
padLeft( '35', 4, '0') // 000035
padLeft('459', 4, '0') // 0000459

Sane behaviour:

padLeft(  '4', 4, '0') // 0004
padLeft( '35', 4, '0') // 0035
padLeft('459', 4, '0') // 0459

Version 0.0.3 on npm.

Hi,

One of my dependencies requires version 0.0.3 on npm. Is it removed?

I am getting the following error when running npm i

npm ERR! Darwin 14.5.0
npm ERR! argv "/usr/local/Cellar/node/5.5.0/bin/node" "/usr/local/bin/npm" "i"
npm ERR! node v5.5.0
npm ERR! npm  v3.5.3

npm ERR! No compatible version found: [email protected]
npm ERR! Valid install targets:
npm ERR! 0.0.9
npm ERR!
npm ERR!
npm ERR! If you need help, you may report this error at:
npm ERR!     <https://github.com/npm/npm/issues>

npm ERR! Please include the following file with any support request:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.