forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/brianvoe/gofakeit/v6/finance.go

214 lines
2.8 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
package gofakeit
import (
"math/rand"
"strconv"
"unicode"
)
const cusipStr = upperStr + numericStr
// CUSIP
2024-04-26 19:30:35 +00:00
func Cusip() string {
2024-04-26 19:30:35 +00:00
return cusip(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
func (f *Faker) Cusip() string {
2024-04-26 19:30:35 +00:00
return cusip(f.Rand)
2024-04-26 19:30:35 +00:00
}
func cusip(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
cusipBytes := make([]byte, 8)
2024-04-26 19:30:35 +00:00
for i := 0; i < len(cusipBytes); i++ {
2024-04-26 19:30:35 +00:00
cusipBytes[i] = byte(cusipStr[r.Intn(len(cusipStr))])
2024-04-26 19:30:35 +00:00
}
baseCusip := string(cusipBytes)
chkDigit := cusipChecksumDigit(baseCusip)
2024-04-26 19:30:35 +00:00
return baseCusip + chkDigit
2024-04-26 19:30:35 +00:00
}
// ISIN
2024-04-26 19:30:35 +00:00
func Isin() string {
2024-04-26 19:30:35 +00:00
return isin(globalFaker.Rand)
2024-04-26 19:30:35 +00:00
}
func (f *Faker) Isin() string {
2024-04-26 19:30:35 +00:00
return isin(f.Rand)
2024-04-26 19:30:35 +00:00
}
func isin(r *rand.Rand) string {
2024-04-26 19:30:35 +00:00
countryCode := CountryAbr()
2024-04-26 19:30:35 +00:00
nsin := cusip(r)
2024-04-26 19:30:35 +00:00
isinChkDig := isinChecksumDigit(countryCode + nsin)
2024-04-26 19:30:35 +00:00
return countryCode + nsin + isinChkDig
2024-04-26 19:30:35 +00:00
}
// cusipChecksumDigit returns the checksum digit for a CUSIP
2024-04-26 19:30:35 +00:00
func cusipChecksumDigit(cusip string) string {
2024-04-26 19:30:35 +00:00
sum := 0
2024-04-26 19:30:35 +00:00
for i, c := range cusip {
2024-04-26 19:30:35 +00:00
v := 0
2024-04-26 19:30:35 +00:00
if unicode.IsDigit(c) {
2024-04-26 19:30:35 +00:00
v = int(c - '0')
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if unicode.IsLetter(c) {
2024-04-26 19:30:35 +00:00
//0-indexed ordinal position of Letter + 10
2024-04-26 19:30:35 +00:00
v = int(c-'A') + 10
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if i%2 != 0 {
2024-04-26 19:30:35 +00:00
// Multiply odd digits by two
2024-04-26 19:30:35 +00:00
v = v * 2
2024-04-26 19:30:35 +00:00
}
sum = sum + int(v/10) + v%10
2024-04-26 19:30:35 +00:00
}
return strconv.Itoa((10 - (sum % 10)) % 10)
2024-04-26 19:30:35 +00:00
}
// isinChecksumDigit returns the checksum digit for an ISIN
2024-04-26 19:30:35 +00:00
func isinChecksumDigit(isin string) string {
2024-04-26 19:30:35 +00:00
isinDigits := make([]int, 0)
2024-04-26 19:30:35 +00:00
for _, c := range isin {
2024-04-26 19:30:35 +00:00
if unicode.IsLetter(c) {
2024-04-26 19:30:35 +00:00
letterVal := int(c) - 55
2024-04-26 19:30:35 +00:00
// Each digit is added as a separate value
2024-04-26 19:30:35 +00:00
isinDigits = append(isinDigits, letterVal/10)
2024-04-26 19:30:35 +00:00
isinDigits = append(isinDigits, letterVal%10)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if unicode.IsDigit(c) {
2024-04-26 19:30:35 +00:00
isinDigits = append(isinDigits, int(c-'0'))
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
oddSum := 0
2024-04-26 19:30:35 +00:00
evenSum := 0
// Take the per digit sum of the digitized ISIN, doubling even indexed digits
2024-04-26 19:30:35 +00:00
for i, d := range isinDigits {
2024-04-26 19:30:35 +00:00
if i%2 == 0 {
2024-04-26 19:30:35 +00:00
elem := 2 * d
2024-04-26 19:30:35 +00:00
if elem > 9 {
2024-04-26 19:30:35 +00:00
// If the element now has two digits, sum those digits
2024-04-26 19:30:35 +00:00
elem = (elem % 10) + (elem / 10)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
evenSum += elem
2024-04-26 19:30:35 +00:00
} else {
2024-04-26 19:30:35 +00:00
oddSum += d
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
return strconv.Itoa((10 - (oddSum+evenSum)%10) % 10)
2024-04-26 19:30:35 +00:00
}
// Lookup Adds
2024-04-26 19:30:35 +00:00
func addFinanceLookup() {
2024-04-26 19:30:35 +00:00
AddFuncLookup("cusip", Info{
Display: "CUSIP",
Category: "finance",
2024-04-26 19:30:35 +00:00
Description: "Unique identifier for securities, especially bonds, in the United States and Canada",
Example: "38259P508",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return cusip(r), nil
2024-04-26 19:30:35 +00:00
},
})
2024-04-26 19:30:35 +00:00
AddFuncLookup("isin", Info{
Display: "ISIN",
Category: "finance",
2024-04-26 19:30:35 +00:00
Description: "International standard code for uniquely identifying securities worldwide",
Example: "CVLRQCZBXQ97",
Output: "string",
2024-04-26 19:30:35 +00:00
Generate: func(r *rand.Rand, m *MapParams, info *Info) (any, error) {
2024-04-26 19:30:35 +00:00
return isin(r), nil
2024-04-26 19:30:35 +00:00
},
})
2024-04-26 19:30:35 +00:00
}