-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathconverters.go
118 lines (100 loc) · 2.61 KB
/
converters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package imagecashletter
import (
"strconv"
"strings"
"time"
)
// converters handles golang to imagecashletter type Converters
type converters struct{}
func (c *converters) parseNumField(r string) (s int) {
s, _ = strconv.Atoi(strings.TrimSpace(r))
return s
}
func (c *converters) parseStringField(r string) (s string) {
s = strings.TrimSpace(r)
return s
}
// stringToBytesField
func (c *converters) stringToBytesField(r string) (b []byte) {
b = []byte(r)
return b
}
// formatYYYYMMDDDate takes a time.Time and returns a string of YYYYMMDD
func (c *converters) formatYYYYMMDDDate(t time.Time) string {
return t.Format("20060102")
}
// parseYYYMMDDDate returns a time.Time when passed time as YYYYMMDD
func (c *converters) parseYYYYMMDDDate(s string) time.Time {
t, _ := time.Parse("20060102", s)
return t
}
// formatSimpleTime returns a string of HHMM when passed a time.Time
func (c *converters) formatSimpleTime(t time.Time) string {
return t.Format("1504")
}
// parseSimpleTime returns a time.Time when passed a string of HHMM
func (c *converters) parseSimpleTime(s string) time.Time {
t, _ := time.Parse("1504", s)
return t
}
// alphaField Alphanumeric and Alphabetic fields are left-justified and space filled.
func (c *converters) alphaField(s string, max uint) string {
ln := uint(len(s))
if ln > max {
return s[:max]
}
rem := max - ln
if !validSizeUint(rem) {
return ""
} else {
s += strings.Repeat(" ", int(rem)) //nolint:gosec
}
return s
}
// numericField right-justified, unsigned, and zero filled
func (c *converters) numericField(n int, max uint) string {
s := strconv.Itoa(n)
ln := uint(len(s))
if ln > max {
return s[ln-max:]
}
rem := max - ln
if !validSizeUint(rem) {
return ""
} else {
s = strings.Repeat("0", int(rem)) + s //nolint:gosec
}
return s
}
// nbsmField is a numeric-blank/special MICR (NBSM) or numeric-blank/special MICR On-Us (NBSMOS)
// which are right-justified and blank filled
func (c *converters) nbsmField(s string, max uint) string {
ln := uint(len(s))
if ln > max {
return s[ln-max:]
}
rem := max - ln
if !validSizeUint(rem) {
return ""
} else {
s = strings.Repeat(" ", int(rem)) + s //nolint:gosec
}
return s
}
// stringField slices to max length and zero filled
func (c *converters) stringField(s string, max uint) string {
ln := uint(len(s))
if ln > max {
return s[:max]
}
rem := max - ln
if !validSizeUint(rem) {
return ""
} else {
s = strings.Repeat("0", int(rem)) + s //nolint:gosec
}
return s
}