Giter Club home page Giter Club logo

left-pad's Introduction

left-pad

String left pad

Build Status

Install

$ npm install left-pad

Usage

const leftPad = require('left-pad')

leftPad('foo', 5)
// => "  foo"

leftPad('foobar', 6)
// => "foobar"

leftPad(1, 2, '0')
// => "01"

leftPad(17, 5, 0)
// => "00017"

NOTE: The third argument should be a single char. However the module doesn't throw an error if you supply more than one chars. See #28.

NOTE: Characters having code points outside of BMP plane are considered two distinct characters. See #58.

left-pad's People

Contributors

100errors avatar anhthii avatar benjamingr avatar camwest avatar cht8687 avatar doersino avatar dubzzz avatar haoel avatar kirushyk avatar lexjacobs avatar nloomans avatar paitoanderson avatar spectejb avatar stevemao avatar zac1st1k avatar zehua 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

left-pad's Issues

Add a License

Would it be possible to add an explicit license to this project? Right now it only states BSD in the package.json file, but it doesn't state which variant of the BSD licenses nor who actually holds the copyright on the code.

We are trying to include proper licenses for all our dependencies.

Regards,
Mattias Amnefelt

TypeError: Cannot read property '1' of undefined

The first argument of leftPad can be a number and a number can have the value NaN. The following TypeError occurs when NaN is passed as the first argument. I expect to throw an error or give a default value when the first argument cannot be left padded.

file:///path/left-pad/index.js:34
  if (ch === ' ' && len < 10) return cache[len] + str;
                                          ^

TypeError: Cannot read property '1' of undefined
    at leftPad (file:///path/left-pad/index.js:34:43)

Wrong size when left padding a unicode string

There is an inconsistency when padding strings containg unicode characters out of BMP plan (ie. code points encoded on two chars in UTF-16).

leftPad('a\u{1f431}b', 4, 'x') => 'a\u{1f431}b' // in: 3 code points, out: 3 code points
leftPad('abc', 4, '\u{1f431}') => '\u{1f431}abc' // in: 3 code points, out: 4 code points

You should maybe specify that left-pad does not handle code points out of BMP plan as single characters.

Failure found using property based testing:
https://runkit.com/dubzzz/5ab9f3d8cc861f0012852eff

left-pad should correctly handle Unicode

One of the falsehoods programmers believe about mono-spaced typefaces is that each character occupies one column, which is terribly wrong. There are both characters that occupy zero columns (like a line-feed or combining accent characters) and characters that occupy two or more columns (like Chinese characters).

left-pad should incorporate code to correctly pad Unicode strings to the specified number of columns, even if the string contains east-asian characters.

npmjs.org tells me that left-pad is not available (404 page)

When building projects on travis, or when searching for left-pad on npmjs.com, both will report that the package cannot be found.

Here is an excerpt from the travis build log

npm ERR! Linux 3.13.0-40-generic
npm ERR! argv "/home/travis/.nvm/versions/node/v4.2.2/bin/node" "/home/travis/.nvm/versions/node/v4.2.2/bin/npm" "install"
npm ERR! node v4.2.2
npm ERR! npm  v2.14.7
npm ERR! code E404
npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/left-pad
npm ERR! 404 
npm ERR! 404 'left-pad' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 It was specified as a dependency of 'line-numbers'
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
npm ERR! Please include the following file with any support request:
npm ERR!     /home/travis/build/coldrye-es/pingo/npm-debug.log
make: *** [deps] Error 1

And here is the standard npmjs.com error page https://www.npmjs.com/package/left-pad

However, if I remove left-pad from my local npm cache and then reinstall it using npm it will happily install [email protected].

Why O(log(n))?

Why your code is O(log(n))?
Can you provide a formal proof?

Create LICENSE file

Would you please add a LICENSE file with the text of the license? There are several different variants of the BSD license, and typically the license text should accompany the source code.

It is not O(log n)

It says in the comments that it is O(log n)

It is not. It must be at least O(n) because each memory location needs to be written to once in order to assign all of them the same value. O(n) is a lower boundary - no algorithm can possibly be faster than that.

In fact, your code is slower than just allocating the memory once and then setting all the characters to the same value. String concatenation takes time, it often cause at least one memory copy.

This code is faster than the concatination-business you are up to:

return pad.repeat(count) + original_string;

I was told to be nice and calm, so I removed the harsh language.

https://dxr.mozilla.org/mozilla-central/source/js/src/builtin/String.js#516-524

Please add a 0.0.3 version.

A dependency of babel, line-numbers is using left-pad version 0.0.3 as a dependency. Can you please add back in a 0.0.3 version to support the legacy features

Getting more done in GitHub with ZenHub

Hola! @hackjus has created a ZenHub account for the stevemao organization. ZenHub is the only project management tool integrated natively in GitHub – created specifically for fast-moving, software-driven teams.


How do I use ZenHub?

To get set up with ZenHub, all you have to do is download the browser extension and log in with your GitHub account. Once you do, you’ll get access to ZenHub’s complete feature-set immediately.

What can ZenHub do?

ZenHub adds a series of enhancements directly inside the GitHub UI:

  • Real-time, customizable task boards for GitHub issues;
  • Multi-Repository burndown charts, estimates, and velocity tracking based on GitHub Milestones;
  • Personal to-do lists and task prioritization;
  • Time-saving shortcuts – like a quick repo switcher, a “Move issue” button, and much more.

Add ZenHub to GitHub

Still curious? See more ZenHub features or read user reviews. This issue was written by your friendly ZenHub bot, posted by request from @hackjus.

ZenHub Board

License clarification for older versions

Hello @stevemao

Currently I'm using the version 0.0.3 of this module and there is no clearly specified the license and I cannot find this version on your repository either. The only information available is the one from package.json which seems to be "license: BSD". What license does apply for this version? Is it BSD? What version of BSD would apply?

Thank you!

Why is your code so complicated?

I can't believe how overly complicated your solution is. This can all be done in 1 line in both ES5 and ES6.

ES6

const leftpad = (str, len, ch = ' ') => Array(Math.max(len - String(str).length + 1, 0)).join(String(ch) ? ch : ' ') + str;

ES5

function leftpad(str, len, ch) { 
  return new Array(Math.max(len - String(str).length + 1, 0)).join(ch != undefined && String(ch) ? ch : ' ') + str; 
}

O(n^2) time

You've opened eyes on the policies of npm regarding to the people who are "going to be banging on your door and taking down your accounts and stuff like that". To show my gratitude I'd like to ask you to fix an issue with O(n^2) execution time, e.g.

module.exports = function(str, len, ch) {
    return new Array(Math.max(0, len - str.length) + 1).join(ch || ' ') + str;
};

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

This issue originally came from pad-left's issue #11.


This issue may target left-pad, pad-left, right-pad, and pad-right modules.


ES6 has evolved a lot so far; now it has String.prototype.padStart and String.prototype.repeat.
However, left-pad 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 leftPad function body.

Original

module.exports = leftPad;

Modified

if("padStart" in String.prototype)
{
  module.exports = function leftPad(str, num, ch) {
    return (str + '').padStart(num, ch);
    // The reason why you don't use `.toString()` would be
    // to deal with `null`s and `undefined`s, right?
  };
}
else
{
  module.exports = leftPad;
}

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

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.