Coder Social home page Coder Social logo

flight-school / money Goto Github PK

View Code? Open in Web Editor NEW
912.0 912.0 34.0 167 KB

A precise, type-safe representation of a monetary amount in a given currency

Home Page: https://flight.school/books/numbers

License: MIT License

Swift 100.00%
currency decimal gyb money swift

money's People

Contributors

mattt avatar sberrevoets avatar shaps80 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

money's Issues

CocoaPods Support

It would be nice to use this from CocoaPods! Here’s a rough draft of a Podspec that seems to work, albeit with a warning that the summary and description are identical:

Pod::Spec.new do |s|
  
  s.name         = "Money"
  s.version      = "1.0.0"
  s.summary      = "A precise, type-safe representation of a monetary amount in a given currency."

  s.description  = <<-DESC
A precise, type-safe representation of a monetary amount in a given currency.
                   DESC

  s.homepage     = "https://gumroad.com/l/swift-numbers"
  s.license      = { :type => "MIT", :file => "LICENSE.md" }


  s.author             = "Mattt"
  s.social_media_url   = "http://twitter.com/mattt"

  s.swift_version = '4.0'
  s.ios.deployment_target = "8.0"
  s.osx.deployment_target = "10.10"
  s.watchos.deployment_target = "2.0"
  s.tvos.deployment_target = "9.0"

  s.source       = { :git => "https://github.com/Flight-School/Money.git", :tag => "#{s.version}" }

  s.source_files  = "Sources/Money/*.swift"

end

Instantiate based on currencyCode

Hi,

I'm not sure if this is the best place to ask this question so bear with me in case this is more of a feature request or just not understanding the library clearly.

Some Background

I am building an app and all currency values are stored and passed in API's using Minor Values (e.g. cents or pence as needed by the currency). I'm using similar libraries for JavaScript and the Backend to be able to manage this.

I was looking for a similar library to understand and use this on Swift.

Usage

I have two steps in what I'm trying to do.

(1) Creating a Money value based on a currencyCode which is passed as a string.

I sense this is something I'm not fully understanding in Swift, but how do I instantiate the Money with the "USD" actually being a string?

(2) Creating the value from a Minor Value

I considered creating this as an extension of your class, which would need to do the following:

  • Understanding which currency is being converted using the currencyCode, then finding the CurrencyType enum which matches this currencyCode
  • Using the CurrencyType. minorUnit to understand how many decimal places there are for this currency and split the integer into a decimal with the correct values (maybe via a string concatenation).
    However, in this case the factory will have to know if it's returning a USD or GBP ... in the return type which causes a compilation error.

Again, apologies if there is something I'm not understanding, but any help or guidance would be much appreciated.

Thanks

Moe

Hashability?

Hashability?

Sure, it may seem like a big one, so if you agree that this ticket would be nice to have, I'll contribute.

Problem

Money's types, like CurrencyType can't be collected in Sets or used in sophisticated cases in general since protocols can't be fancily extended on the dependent's end.

Specifically this issue tries to raise awareness on the necessity of Hashability.

Solution

Make things Hashable that make sense. Like CurrencyType.

Change "currencyCode" JSON coding key to "currency"

Please make the "currencyCode" JSON key configurable to "currency", or please change this key to "currency" for better compatibility between libraries.

The JSON serialization/deserialization of a Money object does not seem to be configurable to another key besides "currencyCode". Whereas, "currency" seems to be the preferred default JSON key for money libraries in other languages (i.e. jackson-datatype-money & dinero). Also, not every library can be configured to use "currencyCode" as the JSON key, and "currency" is a shorter key that offers minor savings in bandwidth and storage costs.

Potential rounding issues decoding from json

This is a fantastic piece of work by the way @mattt and has saved us from having to roll our own solution.

I did discover what could be a potential rounding issue when decoding Decimal's from json. I'm seeing the following test fail

Screen Shot 2020-04-30 at 3 47 53 PM

Our solution to this was to perform the same rounding correction you already perform elsewhere in the code and add it to the decode function.

E.g.

self.amount = Money<Currency>(amount).rounded.amount

So the decode method becomes

Screen Shot 2020-04-30 at 3 59 13 PM

Just thought I'd mention this incase I'm doing something dumb or it isn't dumb and could be useful to someone else.

I have one other question. We'd like to override the behaviour of the encode and decode functions to support decoding from a string and encoding to a single value and not the amount + currency (currently we're only using a single currency, but that could change down the line)

How can I extend or override these methods without forking the repo or plain copying your class? It seems as though you can't inherit from a generic class and override in the usual way.

Anyway thanks again for making this πŸ‘

use currency from locale

Hi,

like the effort here, thx!

One issue that comes up immediately is localization. Is there an easy way to use the currency defined from the current locale?

CocoaPods operations reset SWIFT_VERSION to 4.2

I have a project that includes Money version 1.2.0. The project has 4 targets, 1 run and 3 test targets. All 4 targets have set the Swift Language Version to Swift 5.

I recently started updating CocoaPods library dependencies. I got a build error after updating an unrelated CocoaPods library saying we couldn't add/subtract Money values. Turns out that when I had run pod update <CocoaPods Library Name>, the updated pods project changed the SWIFT_VERSION from 5.0 to 4.2. If I reset the pods project SWIFT_VERSION back to 5.0, the project compiles without errors.

CocoaPods 1.7.0 adds support for multiple Swift versions in the Podspec. It also allows the Podfile to specify a swift version for a pod, using supports_swift_versions '>= 5.0'. We're currently using CocoaPods 1.9.1, so I added that to our podfile and ran pod update. CocoaPods reported the following error:

  • Money-FlightSchool does not specify a Swift version (4.2) that is satisfied by any of targets (<AppTargetName>) integrating it.

It doesn't appear there are any code changes needed to run Money on Swift 5. Can you update the podspec to allow running on the latest Swift versions?

Thank you for considering this!!

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.