Skip to content

Commit

Permalink
chore: factor out inarray
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Musat committed Dec 10, 2022
1 parent 0871b98 commit b3dab09
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
10 changes: 6 additions & 4 deletions internal/node/flatten_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"testing"
)

const testGroup = "test-group"

func TestNode_Flatten(t *testing.T) {
a := require.New(t)

node0 := MakeNode("0", 0)
node1 := MakeNode("1", 0)
node2 := MakeNode("2", 0)
node3 := MakeNode("3", 0)
node0 := MakeNode("0", testGroup, 0)
node1 := MakeNode("1", testGroup, 0)
node2 := MakeNode("2", testGroup, 0)
node3 := MakeNode("3", testGroup, 0)

node0.AddChild(node1)
node0.AddChild(node2)
Expand Down
17 changes: 6 additions & 11 deletions internal/node/level.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package node

import "context"
import (
"context"

"dep-tree/internal/utils"
)

type key int

Expand All @@ -11,15 +15,6 @@ const (
const unknown = -2
const cyclic = -1

func elementInArray[T comparable](value T, array []T) bool {
for _, el := range array {
if value == el {
return true
}
}
return false
}

func hashDep[T any](a *Node[T], b *Node[T]) string {
return a.Id + " -> " + b.Id
}
Expand Down Expand Up @@ -47,7 +42,7 @@ func calculateLevel[T any](
knownCycles, _ := ctx.Value(cycleKey).([]string)
if knownCycles == nil {
knownCycles = []string{}
} else if elementInArray(dep, knownCycles) {
} else if utils.InArray(dep, knownCycles) {
continue
}

Expand Down
18 changes: 9 additions & 9 deletions internal/node/level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
func TestNode_Level(t *testing.T) {
a := require.New(t)

node0 := MakeNode("0", 0)
node1 := MakeNode("1", 0)
node2 := MakeNode("2", 0)
node3 := MakeNode("3", 0)
node0 := MakeNode("0", testGroup, 0)
node1 := MakeNode("1", testGroup, 0)
node2 := MakeNode("2", testGroup, 0)
node3 := MakeNode("3", testGroup, 0)

node0.AddChild(node1, node2)
node1.AddChild(node3)
Expand All @@ -34,11 +34,11 @@ func TestNode_Level(t *testing.T) {
func TestNode_Level_Circular(t *testing.T) {
a := require.New(t)

node0 := MakeNode("0", 0)
node1 := MakeNode("1", 0)
node2 := MakeNode("2", 0)
node3 := MakeNode("3", 0)
node4 := MakeNode("4", 0)
node0 := MakeNode("0", testGroup, 0)
node1 := MakeNode("1", testGroup, 0)
node2 := MakeNode("2", testGroup, 0)
node3 := MakeNode("3", testGroup, 0)
node4 := MakeNode("4", testGroup, 0)

node0.AddChild(node1, node2, node3)
node1.AddChild(node2, node4)
Expand Down
4 changes: 3 additions & 1 deletion internal/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import "github.com/elliotchance/orderedmap/v2"

type Node[T any] struct {
Id string
GroupId string
Data T
Children *orderedmap.OrderedMap[string, *Node[T]]
Parents *orderedmap.OrderedMap[string, *Node[T]]
}

func MakeNode[T any](id string, data T) *Node[T] {
func MakeNode[T any](id string, groupId string, data T) *Node[T] {
return &Node[T]{
Id: id,
Data: data,
GroupId: groupId,
Children: orderedmap.NewOrderedMap[string, *Node[T]](),
Parents: orderedmap.NewOrderedMap[string, *Node[T]](),
}
Expand Down
5 changes: 2 additions & 3 deletions internal/render/board_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package render

import (
"os"

"github.com/stretchr/testify/require"

"path"
"testing"

"github.com/stretchr/testify/require"
)

const RebuildTestsEnv = "REBUILD_TESTS"
Expand Down
10 changes: 10 additions & 0 deletions internal/utils/inarray.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

func InArray[T comparable](value T, array []T) bool {
for _, el := range array {
if value == el {
return true
}
}
return false
}

0 comments on commit b3dab09

Please sign in to comment.