Giter VIP home page Giter VIP logo

go-price-parser's Introduction

Go Price Parser

Don't you find it frustrating that whenever you are trying to parse price from scraped data, you can never just use one simple regex and be done with it?

Wouldn't be great, right?

Every website (and country) deciding to now use a comma, now a dot, and why not a space?!

Well, I was fed up with it and made this little library that just does one thing: you feed it a string with a price, and it gives you back a nicely formatted int.

Why an int, you might wonder?

Well, that's easy to deal with and not get crazy with all those float shenanigans.

Features ๐Ÿš€

  • Parses a "dirty" price string into an integer (e.g. "42,069.23 USD" => 4206923)
  • Returns values as either directly integer or as Price struct (allows some extra formatting features)

Installation ๐Ÿ“ฆ

Install it as usual with:

  go get github.com/alexferrari88/go-price-parser

Usage/Examples ๐Ÿ”ง

You can have the return values as either int or as the Price struct defined in this library

Return value as int

This is handy if you want to pipe the parsed price in a library such as Money.

scrapedPrice := "42.99 USD"
price, err := priceParser.IntFromString(scrapedPrice)
if err != nill {
    panic(err)
}

fmt.Println(price) // Output: 4299

Return value as Price struct

You can use this if you want to keep the currency or for some formatting (see below).

scrapedPrice := "42.99 USD"
price, err := priceParser.PriceFromString(scrapedPrice)
if err != nill {
    panic(err)
}

fmt.Println(price.Amount) // Output: 4299

Other handy functions in the package

If you keep the result of the function as Price struct, you can also use the functions String() and Float().

Examples

scrapedPrice := "42.99 USD"
price, err := priceParser.PriceFromString(scrapedPrice)
if err != nill {
    panic(err)
}

fmt.Println(price.Amount.String()) // Output: "42.99 USD"
fmt.Println(price.Amount.Float()) // Output: "42.99"

To Do โœ…

  • Implement currency detection
  • Add more documentation to the code
  • Create tests
  • Optimize code
  • Improve README.md

Acknowledgements ๐Ÿค—

License ๐Ÿ“

MIT

go-price-parser's People

Contributors

alexferrari88 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  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.