GithubHelp home page GithubHelp logo

stevenmg / swiftleftpad Goto Github PK

View Code? Open in Web Editor NEW

This project forked from coryalder/swiftleftpad

0.0 2.0 0.0 4 KB

An implementation of npm's notorious left-pad module in Swift. Pad a string with characters on the left.

Swift 100.00%

swiftleftpad's Introduction

SwiftLeftpad

A lot of people have interpreted the recent npm kerfuffle aruond kik, and left-pad as saying everyone should have written their own leftpad function, instead of relying on a 3rd party dependency. It is only 11 lines of code (in js). I have a different view. One of the things we teach beginners is Do not Repeat Yourself. We tell them if they write the code once, and call it from wherever it's needed, we have only one place where we need to fix our bugs. No bit of code is small enough or simple enough to be guaranteed bug free, and centralizing functions, like leftpad, means we only have to debug them once.

The reality is, in the absence of a left-pad module, most people will go to Stack Overflow, search "left pad string swift" and copy-paste the first high-voted answer they find. Now, SO is a good tool, and if I find a bug in the implementation there I can fix it, but that change doesn't propagate out to everybody who's copied it into their "utils" library.

With all that in mind, I created a Swift version of left-pad, which I hope you'll consider using if you have this problem. It's currently only available using Swift Package Manager, but pull requests for CocoaPods and Carthage will be considered. This is also a direct port of the npm version of left-pad, and has some of the same bugs and inefficiencies as the source. Pull requests welcome, let's fix it once and never again.

And yes, I know about stringByPaddingToLength(newLength: Int, withString padString: String, startingAtIndex padIndex: Int).

Installation

Swift Package Manager

Add SwiftLeftpad to your Package.swift file:

import PackageDescription

let package = Package(
    name: "YourPackageName",
    dependencies: [
        .Package(url: "https://github.com/coryalder/SwiftLeftpad.git", majorVersion: 0),
    ]
)

Usage

import SwiftLeftpad

print("testing 123".leftpad(20))
// => "        testing 123"

print(" winner".leftpad(20, character: "#"))
// => "############# winner"

By default, SwiftLeftpad will use a single space as the padding character, but you can supply your own.

swiftleftpad's People

Contributors

coryalder avatar

Watchers

James Cloos avatar Steven Glick avatar

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.