Skip to content

Commit

Permalink
Removed deprecated functions slice and contains
Browse files Browse the repository at this point in the history
  • Loading branch information
odino committed Apr 4, 2020
1 parent b9e0354 commit 5dafb9a
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 175 deletions.
30 changes: 0 additions & 30 deletions docs/types/array.md
Expand Up @@ -131,21 +131,6 @@ called on homogeneous arrays of numbers.
## Supported functions
### contains(e)
> This function is deprecated and might be removed in future versions.
>
> Use the "in" operator instead: 3 in [1, 2, 3]
Checks whether `e` is present in the array. `e` can only be
a string or number and the array needs to be a homogeneous array
of strings or numbers:
``` py
[1, 2, 3].contains(3) # true
[1, 2, 3].contains(4) # false
```
### chunk(size)
Splits the array into chunks of the given size:
Expand Down Expand Up @@ -265,21 +250,6 @@ a.shift() # 1
a # [2, 3]
```
### slice(start, end)
Returns a portion of the array, from `start` to `end`:
``` py
(1..10).slice(0, 3) # [1, 2, 3]"
```
If `start` is negative, it slices from the end of the string,
back as many characters as the value of `start`:
``` bash
(1..10).slice(-3, 0) # [8, 9, 10]"
```
### some(f)
Returns true when at least one of the elements in the array
Expand Down
33 changes: 0 additions & 33 deletions docs/types/string.md
Expand Up @@ -318,20 +318,6 @@ Parses the string as JSON, returning a [hash](/types/hash):
{x: 10, y: 20}
```

### contains(str)

> This function is deprecated and might be removed in future versions.
>
> Use the "in" operator instead: `"str" in "string"`

Checks whether `str` is present in the string:

``` bash
"string".contains("ing") # true
"string".contains("ong") # false
```

### str()

Identity:
Expand Down Expand Up @@ -457,25 +443,6 @@ Returns the last index at which `str` is found:
"string string".last_index("g") # 13
```

### slice(start, end)

> This function is deprecated and might be removed in future versions.
>
> Use the index notation instead: `"string"[0:3]`
Returns a portion of the string, from `start` to `end`:

``` bash
"string".slice(0, 3) # "str"
```

If `start` is negative, it slices from the end of the string,
back as many characters as the value of `start`:

``` bash
"string".slice(-3, 0) # "ing"
```

### camel()

Converts the string to camelCase:
Expand Down
31 changes: 1 addition & 30 deletions evaluator/builtin_functions_test.go
Expand Up @@ -65,35 +65,6 @@ func TestIsNumber(t *testing.T) {
testBuiltinFunction(tests, t)
}

func TestSlice(t *testing.T) {
tests := []Tests{
{`[1,2,3].slice(0, 0)`, []int{1, 2, 3}},
{`[1,2,3].slice(1, 0)`, []int{2, 3}},
{`[1,2,3].slice(1, 2)`, []int{2}},
{`[1,2,3].slice(0, 6)`, []int{1, 2, 3}},
{`[1,2,3].slice(10, 10)`, []int{}},
{`[1,2,3].slice(10, 20)`, []int{}},
{`[1,2,3].slice(-1, 0)`, []int{3}},
{`[1,2,3].slice(-20, 0)`, []int{1, 2, 3}},
{`[1,2,3].slice(-20, 2)`, []int{1, 2}},
{`[1,2,3].slice(-1, 3)`, []int{3}},
{`[1,2,3].slice(-1, 1)`, []int{3}},
{`"abc".slice(0, 0)`, "abc"},
{`"abc".slice(1, 0)`, "bc"},
{`"abc".slice(1, 2)`, "b"},
{`"abc".slice(0, 6)`, "abc"},
{`"abc".slice(10, 10)`, ""},
{`"abc".slice(10, 20)`, ""},
{`"abc".slice(-1, 0)`, "c"},
{`"abc".slice(-20, 0)`, "abc"},
{`"abc".slice(-20, 2)`, "ab"},
{`"abc".slice(-1, 3)`, "c"},
{`"abc".slice(-1, 1)`, "c"},
}

testBuiltinFunction(tests, t)
}

func TestType(t *testing.T) {
tests := []Tests{
{`type("SOME")`, "STRING"},
Expand Down Expand Up @@ -534,7 +505,7 @@ func TestEval(t *testing.T) {

func TestMisc(t *testing.T) {
tests := []Tests{
{`pwd().split("").reverse().slice(0, 33).reverse().join("").replace("\\", "/", -1).suffix("/evaluator")`, true}, // Little trick to get travis to run this test, as the base path is not /go/src/
{`pwd().split("").reverse()[0:33].reverse().join("").replace("\\", "/", -1).suffix("/evaluator")`, true}, // Little trick to get travis to run this test, as the base path is not /go/src/
{`cwd = cd(); cwd == pwd()`, true},
{`cwd = cd("path/to/nowhere"); cwd == pwd()`, false},
{`lines("a
Expand Down
82 changes: 0 additions & 82 deletions evaluator/functions.go
Expand Up @@ -278,11 +278,6 @@ func getFns() map[string]*object.Builtin {
Types: []string{object.ARRAY_OBJ},
Fn: uniqueFn,
},
// contains("str", "tr")
"contains": &object.Builtin{
Types: []string{object.ARRAY_OBJ, object.STRING_OBJ},
Fn: containsFn,
},
// str(1)
"str": &object.Builtin{
Types: []string{},
Expand Down Expand Up @@ -362,11 +357,6 @@ func getFns() map[string]*object.Builtin {
Types: []string{object.STRING_OBJ},
Fn: lastIndexFn,
},
// slice("abcc", 0, -1)
"slice": &object.Builtin{
Types: []string{object.STRING_OBJ, object.ARRAY_OBJ},
Fn: sliceFn,
},
// shift([1,2,3])
"shift": &object.Builtin{
Types: []string{object.ARRAY_OBJ},
Expand Down Expand Up @@ -1646,53 +1636,6 @@ func uniqueFn(tok token.Token, env *object.Environment, args ...object.Object) o
return &object.Array{Elements: result}
}

// contains("str", "tr")
func containsFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
err := validateArgs(tok, "contains", args, 2, [][]string{{object.STRING_OBJ, object.ARRAY_OBJ}, {object.STRING_OBJ, object.NUMBER_OBJ}})
if err != nil {
return err
}

switch arg := args[0].(type) {
case *object.String:
needle, ok := args[1].(*object.String)

if ok {
return &object.Boolean{Token: tok, Value: strings.Contains(arg.Value, needle.Value)}
}
case *object.Array:
var found bool

switch needle := args[1].(type) {
case *object.String:
for _, v := range arg.Elements {
if v.Inspect() == needle.Value && v.Type() == object.STRING_OBJ {
found = true
break // Let's get outta here!
}
}

return &object.Boolean{Token: tok, Value: found}
case *object.Number:
for _, v := range arg.Elements {
// Quite ghetto but also the easiest way out
// Instead of doing type checking on the argument,
// we received back its string representation.
// If they match, we then check that its type was
// integer.
if v.Inspect() == strconv.Itoa(int(needle.Value)) && v.Type() == object.NUMBER_OBJ {
found = true
break // Let's get outta here!
}
}

return &object.Boolean{Token: tok, Value: found}
}
}

return &object.Boolean{Token: tok, Value: false}
}

// str(1)
func strFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
err := validateArgs(tok, "str", args, 1, [][]string{})
Expand Down Expand Up @@ -1918,31 +1861,6 @@ func lastIndexFn(tok token.Token, env *object.Environment, args ...object.Object
return &object.Number{Token: tok, Value: float64(i)}
}

// slice("abcc", 0, -1)
func sliceFn(tok token.Token, env *object.Environment, args ...object.Object) object.Object {
err := validateArgs(tok, "slice", args, 3, [][]string{{object.STRING_OBJ, object.ARRAY_OBJ}, {object.NUMBER_OBJ}, {object.NUMBER_OBJ}})
if err != nil {
return err
}

start := int(args[1].(*object.Number).Value)
end := int(args[2].(*object.Number).Value)

switch arg := args[0].(type) {
case *object.String:
s := arg.Value
start, end := sliceStartAndEnd(len(s), start, end)

return &object.String{Token: tok, Value: s[start:end]}
case *object.Array:
start, end := sliceStartAndEnd(len(arg.Elements), start, end)

return &object.Array{Elements: arg.Elements[start:end]}
}

return NULL
}

// Clamps start and end arguments to the slice
// function. When you slice "abc" you can have
// start 10 and end -20...
Expand Down

0 comments on commit 5dafb9a

Please sign in to comment.