func ExampleToDelimiter() {
	ntd1 := gonumbers.ToDelimiter()
	fmt.Println(ntd1.Perform(1234567890))

	ntd2 := gonumbers.ToDelimiter()
	ntd2.Options(gonumbers.Separator("."), gonumbers.Delimiter(","))
	fmt.Println(ntd2.Perform(1234567890.506))

	ntd3 := gonumbers.ToDelimiter()
	ntd3.Options(gonumbers.Separator("-"), gonumbers.Delimiter(":"))
	fmt.Println(ntd3.Perform(1234567890.34))

	ntd4 := gonumbers.ToDelimiter()
	ntd4.Options(gonumbers.Separator("."), gonumbers.Delimiter(""))
	fmt.Println(ntd4.Perform(1234567890.34))

	ntd5 := gonumbers.ToDelimiter()
	ntd5.Options(gonumbers.Separator(""), gonumbers.Delimiter(""))
	fmt.Println(ntd5.Perform(1234567890.34))

	// Output:
	// 1,234,567,890
	// 1,234,567,890.506
	// 1:234:567:890-34
	// 1234567890.34
	// 123456789034
}
Пример #2
0
func Test_ToCurrency(t *testing.T) {

	for _, test := range testsNTC {
		ntc := gonumbers.ToCurrency()

		if test.addPrecision {
			ntc.Options(gonumbers.Precision(test.precision))
		}

		if test.addUnit {
			ntc.Options(gonumbers.Unit(test.unit))
		}

		if test.addSeparator {
			ntc.Options(gonumbers.Separator(test.separator))
		}

		if test.addDelimiter {
			ntc.Options(gonumbers.Delimiter(test.delimiter))
		}

		assert(t, test.out, ntc.Perform(test.in))
	}

}
func Test_ToDelimiter(t *testing.T) {

	tests := []struct {
		in           float64
		addSeparator bool
		separator    string
		addDelimiter bool
		delimiter    string
		out          string
	}{
		{in: 1234567890, out: "1,234,567,890"},
		{
			in:           1234567890.506,
			addSeparator: true,
			separator:    ".",
			addDelimiter: true,
			delimiter:    ",",
			out:          "1,234,567,890.506",
		},
		{
			in:           1234567890.34,
			addSeparator: true,
			separator:    "-",
			addDelimiter: true,
			delimiter:    ":",
			out:          "1:234:567:890-34",
		},
		{
			in:           1234567890.34,
			addSeparator: true,
			separator:    ".",
			addDelimiter: true,
			delimiter:    "",
			out:          "1234567890.34",
		},
		{
			in:           1234567890.34,
			addSeparator: true,
			separator:    "",
			addDelimiter: true,
			delimiter:    "",
			out:          "123456789034",
		},
	}

	for _, test := range tests {
		ntd := gonumbers.ToDelimiter()
		if test.addSeparator {
			ntd.Options(gonumbers.Separator(test.separator))
		}
		if test.addDelimiter {
			ntd.Options(gonumbers.Delimiter(test.delimiter))
		}
		assert(t, test.out, ntd.Perform(test.in))
	}

}
Пример #4
0
func ExampleToCurrency() {
	ntc1 := gonumbers.ToCurrency()
	res1 := ntc1.Perform(1234567890.50)
	fmt.Println(res1)

	ntc2 := gonumbers.ToCurrency()
	ntc2.Options(gonumbers.Precision(2))
	res2 := ntc2.Perform(1234567890.506)
	fmt.Println(res2)

	ntc3 := gonumbers.ToCurrency()
	ntc3.Options(gonumbers.Precision(2), gonumbers.Unit("$"), gonumbers.Separator("."))
	res3 := ntc3.Perform(1234567890)
	fmt.Println(res3)

	ntc4 := gonumbers.ToCurrency()
	ntc4.Options(gonumbers.Precision(3), gonumbers.Unit("CAD$"), gonumbers.Separator("."), gonumbers.Delimiter(","))
	res4 := ntc4.Perform(1234567890.506)
	fmt.Println(res4)

	ntc5 := gonumbers.ToCurrency()
	ntc5.Options(gonumbers.Precision(2), gonumbers.Separator("."), gonumbers.Delimiter(""))
	res5 := ntc5.Perform(1234567890.50)
	fmt.Println(res5)

	ntc6 := gonumbers.ToCurrency()
	ntc6.Options(gonumbers.Precision(2), gonumbers.Separator(","), gonumbers.Delimiter("."))
	res6 := ntc6.Perform(1234567890.506)
	fmt.Println(res6)

	// Output:
	// $1,234,567,890.50
	// $1,234,567,890.51
	// $1,234,567,890.00
	// CAD$1,234,567,890.506
	// $1234567890.50
	// $1.234.567.890,51
}
Пример #5
0
func Test_ToPhone(t *testing.T) {

	ntph_err1 := gonumbers.ToPhone()
	ntph_err1_resp, err1 := ntph_err1.Perform("good")
	assert_not(t, nil, err1)
	contains(t, err1.Error(), "The value should an integer.")
	assert(t, "", ntph_err1_resp)

	ntph_err2 := gonumbers.ToPhone()
	ntph_err2_resp, err2 := ntph_err2.Perform("123abc456")
	assert_not(t, nil, err2)
	contains(t, err2.Error(), "The value should an integer.")
	assert(t, "", ntph_err2_resp)

	tests := []struct {
		in             interface{}
		out            string
		delimiter      string
		addDelimiter   bool
		areaCode       bool
		addAreaCode    bool
		extension      string
		addExtension   bool
		countryCode    string
		addCountryCode bool
		digitsSize     int
		addDigitsSize  bool
	}{
		{in: 5551234, out: "555-1234"},
		{in: 1235551234, out: "123-555-1234"},
		{
			in:          1235551234,
			addAreaCode: true,
			areaCode:    true,
			out:         "(123) 555-1234",
		},
		{
			in:             1235551234,
			addAreaCode:    true,
			areaCode:       true,
			addCountryCode: true,
			countryCode:    "1",
			out:            "+1(123) 555-1234",
		},
		{
			in:             1235551234,
			addCountryCode: true,
			countryCode:    "1",
			out:            "+1-123-555-1234",
		},
		{
			in:          5551234,
			addAreaCode: true,
			areaCode:    true,
			out:         "555-1234",
		},
		{
			in:             1234555556789,
			addAreaCode:    true,
			areaCode:       true,
			addCountryCode: true,
			countryCode:    "1",
			addExtension:   true,
			extension:      "4545",
			out:            "+1(123455) 555-6789 x 4545",
		},
		{
			in:             1234555556789,
			addAreaCode:    true,
			areaCode:       true,
			addCountryCode: true,
			countryCode:    "1",
			addExtension:   true,
			extension:      "4545",
			addDigitsSize:  true,
			digitsSize:     4,
			out:            "+1(12345) 5555-6789 x 4545",
		},
		{
			in:             1234555556789,
			addAreaCode:    true,
			areaCode:       true,
			addCountryCode: true,
			countryCode:    "1",
			addExtension:   true,
			extension:      "4545",
			addDigitsSize:  true,
			digitsSize:     5,
			out:            "+1(1234) 55555-6789 x 4545",
		},
		{
			in:             1234555556789,
			addDelimiter:   true,
			delimiter:      ",",
			addAreaCode:    true,
			areaCode:       true,
			addCountryCode: true,
			countryCode:    "55",
			addDigitsSize:  true,
			digitsSize:     5,
			out:            "+55(1234) 55555,6789",
		},
	}

	for _, test := range tests {
		ntph := gonumbers.ToPhone()

		if test.addDelimiter {
			ntph.Options(gonumbers.Delimiter(test.delimiter))
		}

		if test.addAreaCode {
			ntph.Options(gonumbers.AreaCode(test.areaCode))
		}

		if test.addExtension {
			ntph.Options(gonumbers.Extension(test.extension))
		}

		if test.addCountryCode {
			ntph.Options(gonumbers.CountryCode(test.countryCode))
		}

		if test.addDigitsSize {
			ntph.Options(gonumbers.DigitsSize(test.digitsSize))
		}

		ntph_final, err := ntph.Perform(test.in)
		assert(t, nil, err)
		assert(t, test.out, ntph_final)
	}
}
Пример #6
0
func ExampleToPhone() {
	ntph1 := gonumbers.ToPhone()
	resp1, err1 := ntph1.Perform(5551234)
	if err1 != nil {
		fmt.Println(err1)
	}
	fmt.Println(resp1)

	ntph2 := gonumbers.ToPhone()
	resp2, err2 := ntph2.Perform(1235551234)
	if err2 != nil {
		fmt.Println(err2)
	}
	fmt.Println(resp2)

	ntph3 := gonumbers.ToPhone()
	ntph3.Options(gonumbers.AreaCode(true))
	resp3, err3 := ntph3.Perform(1235551234)
	if err3 != nil {
		fmt.Println(err3)
	}
	fmt.Println(resp3)

	ntph4 := gonumbers.ToPhone()
	ntph4.Options(gonumbers.AreaCode(true), gonumbers.CountryCode("1"))
	resp4, err4 := ntph4.Perform(1235551234)
	if err4 != nil {
		fmt.Println(err4)
	}
	fmt.Println(resp4)

	ntph5 := gonumbers.ToPhone()
	ntph5.Options(gonumbers.CountryCode("1"))
	resp5, err5 := ntph5.Perform(1235551234)
	if err5 != nil {
		fmt.Println(err5)
	}
	fmt.Println(resp5)

	ntph6 := gonumbers.ToPhone()
	ntph6.Options(gonumbers.AreaCode(true), gonumbers.CountryCode("1"), gonumbers.Extension("4545"))
	resp6, err6 := ntph6.Perform(1235551234)
	if err6 != nil {
		fmt.Println(err6)
	}
	fmt.Println(resp6)

	ntph7 := gonumbers.ToPhone()
	ntph7.Options(gonumbers.AreaCode(true), gonumbers.CountryCode("1"), gonumbers.Extension("4545"), gonumbers.DigitsSize(5))
	resp7, err7 := ntph7.Perform(1234555556789)
	if err7 != nil {
		fmt.Println(err7)
	}
	fmt.Println(resp7)

	ntph8 := gonumbers.ToPhone()
	ntph8.Options(gonumbers.AreaCode(true), gonumbers.CountryCode("55"), gonumbers.Delimiter(","), gonumbers.DigitsSize(5))
	resp8, err8 := ntph8.Perform(1234555556789)
	if err8 != nil {
		fmt.Println(err8)
	}
	fmt.Println(resp8)

	// Output:
	// 555-1234
	// 123-555-1234
	// (123) 555-1234
	// +1(123) 555-1234
	// +1-123-555-1234
	// +1(123) 555-1234 x 4545
	// +1(1234) 55555-6789 x 4545
	// +55(1234) 55555,6789

}
func Test_ToHumanSize(t *testing.T) {

	nths1 := gonumbers.ToHumanSize()
	nths1.Options(gonumbers.Prefix("ahhaha"))
	nths1_final, err1 := nths1.Perform(123)
	assert_not(t, nil, err1)
	contains(t, err1.Error(), "Prefix must be binary or si.")
	assert(t, "", nths1_final)

	tests := []struct {
		in           float64
		addPrecision bool
		precision    int
		addSeparator bool
		separator    string
		addDelimiter bool
		delimiter    string
		addPrefix    bool
		prefix       string
		out          string
	}{
		{
			in:  333,
			out: "333 Bytes",
		},
		{
			in:  1234,
			out: "1.21 KB",
		},
		{
			in:  1234567890,
			out: "1.15 GB",
		},
		{
			in:           1234567,
			addPrecision: true,
			precision:    2,
			out:          "1.2 MB",
		},
		{
			in:           524288000,
			addPrecision: true,
			precision:    5,
			out:          "500 MB",
		},
		{
			in:           1234567,
			addPrecision: true,
			precision:    2,
			addSeparator: true,
			separator:    ",",
			out:          "1,2 MB",
		},
	}

	for _, test := range tests {
		nths := gonumbers.ToHumanSize()
		if test.addPrecision {
			nths.Options(gonumbers.Precision(test.precision))
		}
		if test.addSeparator {
			nths.Options(gonumbers.Separator(test.separator))
		}
		if test.addDelimiter {
			nths.Options(gonumbers.Delimiter(test.delimiter))
		}
		if test.addPrefix {
			nths.Options(gonumbers.Prefix(test.prefix))
		}
		nths_final, err := nths.Perform(test.in)
		assert(t, nil, err)
		assert(t, test.out, nths_final)
	}

}