From dffdb836e3ffee1056793cd7b719d76e53bb2597 Mon Sep 17 00:00:00 2001 From: Raphael Pour Date: Thu, 3 Feb 2022 22:02:53 +0100 Subject: [PATCH] float: adds to integer object function --- docs/content/docs/literals/float.md | 14 ++++++++++++++ object/float.go | 14 ++++++++++++++ object/float_test.go | 1 + 3 files changed, 29 insertions(+) diff --git a/docs/content/docs/literals/float.md b/docs/content/docs/literals/float.md index 2e45c04..640f6c8 100644 --- a/docs/content/docs/literals/float.md +++ b/docs/content/docs/literals/float.md @@ -18,6 +18,20 @@ Returns self +### plz_i() +> Returns `INTEGER` + +Converts the float into an integer. + + +```js +🚀 > a = 123.456 +=> 123.456 +🚀 > a.plz_i() +=> "123" +``` + + ### plz_s() > Returns `STRING` diff --git a/object/float.go b/object/float.go index 90111ea..70cfe1c 100644 --- a/object/float.go +++ b/object/float.go @@ -48,6 +48,20 @@ func init() { return NewString(f.toString()) }, }, + "plz_i": ObjectMethod{ + description: "Converts the float into an integer.", + example: `🚀 > a = 123.456 +=> 123.456 +🚀 > a.plz_i() +=> "123"`, + method: func(o Object, args []Object) Object { + f := o.(*Float) + return NewInteger(int64(f.Value)) + }, + returnPattern: [][]string{ + []string{INTEGER_OBJ}, + }, + }, } } diff --git a/object/float_test.go b/object/float_test.go index 5d5986f..857df35 100644 --- a/object/float_test.go +++ b/object/float_test.go @@ -24,6 +24,7 @@ func TestFloatObjectMethods(t *testing.T) { tests := []inputTestCase{ {`2.1.plz_s()`, "2.1"}, {`2.1.plz_f()`, 2.1}, + {`2.1.plz_i()`, 2}, {`10.0.type()`, "FLOAT"}, {`2.2.nope()`, "undefined method `.nope()` for FLOAT"}, {`(2.0.wat().lines().size() == 2.0.methods().size() + 1).plz_s()`, "true"},