Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zzwx/interval
An almost useless utility for normalizing a numeric range, with a wrapping function useful for polar coordinates. It's exploring go 1.x code generation. It is a clone of a JavaScript project by James Talmage (https://github.com/jamestalmage/normalize-range).
https://github.com/zzwx/interval
generated go interval
Last synced: 13 days ago
JSON representation
An almost useless utility for normalizing a numeric range, with a wrapping function useful for polar coordinates. It's exploring go 1.x code generation. It is a clone of a JavaScript project by James Talmage (https://github.com/jamestalmage/normalize-range).
- Host: GitHub
- URL: https://github.com/zzwx/interval
- Owner: zzwx
- License: mit
- Created: 2020-07-15T01:27:12.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-10-10T16:37:03.000Z (over 3 years ago)
- Last Synced: 2024-11-13T23:32:50.816Z (about 2 months ago)
- Topics: generated, go, interval
- Language: Go
- Homepage: https://pkg.go.dev/github.com/zzwx/interval
- Size: 97.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-golang-repositories - interval - range). (Repositories)
README
[![https://github.com/zzwx/interval](./doc/gobadge.svg)](https://pkg.go.dev/github.com/zzwx/interval)
# Interval
An almost useless utility for normalizing a numeric range, with a wrapping function for polar coordinates, implemented using `go generate`.
It is a **golang clone** of a JavaScript project by [James Talmage](https://github.com/jamestalmage/normalize-range).
For dealing with the strict typing in Go, functions were auto-generated for all of the following types:
* `int`
* `int64`
* `int32`
* `int16`
* `int8`
* `uint`
* `uint64`
* `uint32`
* `uint16`
* `uint8`
* `float32`
* `float64`## Motivation
This approach is inspired by [Rob Pike's article](https://blog.golang.org/generate) on code generation. Until generics are implemented, this is simply an example of code generation, exploiting templates in this case.
## Installation
```
go get -u github.com/zzwx/interval
```## Usage
```go
package mainimport (
"fmt""github.com/zzwx/interval"
)func main() {
fmt.Println(interval.WrapInt(0, 360, 400)) //=> 40
fmt.Println(interval.WrapInt(0, 360, -90)) //=> 270
fmt.Println(interval.ClampInt(0, 100, 500)) //=> 100
fmt.Println(interval.ClampInt(0, 100, -20)) //=> 0r := interval.NewRangeFloat64(0, 100, false, false)
fmt.Println(r.Wrap(120)) //=> 20
fmt.Println(r.Validate(120)) //=> 0, error(120 is outside of range [0,100])
fmt.Println(r.Test(120)) //=> false
fmt.Println(r) //=> [0,100] (uses Stringer interface)
}
```[Go Playground](https://play.golang.org/p/c_cqte_YoAe)
## API
https://pkg.go.dev/github.com/zzwx/interval
## License
Original JavaScript author: [James Talmage](https://github.com/jamestalmage/normalize-range)
MIT © [Anton Veretennikov](https://github.com/zzwx)