Coder Social home page Coder Social logo

percentage's Introduction

Percentage

A percentage type for Swift

Makes percentages more readable and type-safe, for example, for APIs that currently accept a fraction Double.

-.opacity(0.45)
+.opacity(45%)

Install

Add the following to Package.swift:

.package(url: "https://github.com/sindresorhus/Percentage", from: "1.2.0")

Or add the package in Xcode.

Usage

See the source for docs.

import Percentage

10% + 5.5%
//=> 15.5%

-10% / 2
//=> -5%

(40% + 93%) * 3
//=> 399%

50% * 50%
//=> 25%

30% > 25%
//=> true

50%.of(200)
//=> 100

Percentage(50)
//=> 50%

Percentage(fraction: 0.5)
//=> 50%

50%.fraction
//=> 0.5

10%.rawValue
//=> 10

110%.clampedZeroToHundred
//=> 100%

print("\(1%)")
//=> "1%"

Percent.random(in: 10%...20%)
//=> "14.3%"

The type conforms to Hashable, Codable, RawRepresentable, Comparable, ExpressibleByFloatLiteral, ExpressibleByIntegerLiteral, Numeric, and supports all the arithmetic operators.

Codable

The percentage value is encoded as a single value:

struct Foo: Codable {
	let alpha: Percentage
}

let foo = Foo(alpha: 1%)
let data = try! JSONEncoder().encode(foo)
let string = String(data: data, encoding: .utf8)!

print(string)
//=> "{\"alpha\":1}"

FAQ

Can you support Carthage and CocoaPods?

No, but you can still use Swift Package Manager for this package even though you mainly use Carthage or CocoaPods.

Related

percentage's People

Contributors

alexbinary avatar evanhahn avatar irisdelaluna avatar magauran avatar sindresorhus 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

percentage's Issues

Build error: "Use of undeclared type 'CGFloat'"

Just tried pulling this into a new Xcode 11.1 project using the GUI SPM flow, and got this build error.

Adding

import CoreGraphics

at the top of Percent.swift fixed it for me. Happy to submit a PR if you want it!

Feedback

Is this a good idea? Any downsides with such a type? Would it make sense to propose adding this kind of type to Swift itself?

Add overloads for Swift and Cocoa APIs?

Codable Implementation

Hey Sindre, this is a cool library! If I'm reading the source code correctly, suppose I had the following struct:

struct SomeObject: Codable {
    let alpha: Percent
}

That object would be encoded as below (and require the below JSON to decode):

{
    alpha: {
        rawValue: 10
    }
}

it may be a nice enhancement to override the default Encodable/Decodable implementations to parse that struct as:

{
    alpha: 0.1
}

I feel this is more likely what you'd get from an API. Up to you whether you want to keep the implementation as is, entirely change it to the above, or maybe provide some static variable to allow users to choose which Codable representation they want.

Add more useful properties

Some ideas:

  • .clampedZeroToHundred which clamps the percentage to 0...100.
  • Percent.from(100, of: 200) //=> 50%
  • Find the original value before a percent increase. E.g. If a value is 120 after a 40% increase, what is the original value before the increase?
  • Find the original value before a percent decrease. E.g. If a value is 106 after a 12% decrease, what is the original value before the decrease?
  • "x IS y% OF". E.g. 67 is 90% of what?

What else?

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.