-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add maps.SortedKeys Signed-off-by: Carlos Alexandro Becker <[email protected]> * build: go version, build * chore: fix go.work Signed-off-by: Carlos Alexandro Becker <[email protected]> * chore: undo unwanted changes Signed-off-by: Carlos Alexandro Becker <[email protected]> * docs: fix merge * test: fix Signed-off-by: Carlos Alexandro Becker <[email protected]> * fix: no deps Signed-off-by: Carlos Alexandro Becker <[email protected]> * test: fix --------- Signed-off-by: Carlos Alexandro Becker <[email protected]>
- Loading branch information
Showing
9 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# auto-generated by scripts/dependabot. DO NOT EDIT. | ||
name: maps | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
paths: | ||
- exp/maps/** | ||
- .github/workflows/maps.yml | ||
|
||
jobs: | ||
build: | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
runs-on: ${{ matrix.os }} | ||
defaults: | ||
run: | ||
working-directory: ./exp/maps | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-go@v5 | ||
with: | ||
go-version-file: ./exp/maps/go.mod | ||
cache: true | ||
cache-dependency-path: ./exp/maps/go.sum | ||
- run: go build -v ./... | ||
- run: go test -race -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/charmbracelet/x/exp/maps | ||
|
||
go 1.21 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package maps | ||
|
||
import ( | ||
"cmp" | ||
"slices" | ||
) | ||
|
||
// SortedKeys returns the keys of the map m. | ||
// The keys will be sorted. | ||
func SortedKeys[M ~map[K]V, K cmp.Ordered, V any](m M) []K { | ||
r := Keys(m) | ||
slices.Sort(r) | ||
return r | ||
} | ||
|
||
// Keys returns the keys of the map m. | ||
func Keys[M ~map[K]V, K cmp.Ordered, V any](m M) []K { | ||
r := make([]K, 0, len(m)) | ||
for k := range m { | ||
r = append(r, k) | ||
} | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package maps | ||
|
||
import ( | ||
"slices" | ||
"testing" | ||
) | ||
|
||
func TestSortedKeys(t *testing.T) { | ||
m := map[string]int{ | ||
"foo": 1, | ||
"bar": 10, | ||
"aaaaa": 11, | ||
} | ||
|
||
keys := SortedKeys(m) | ||
if slices.Compare(keys, []string{"aaaaa", "bar", "foo"}) != 0 { | ||
t.Fatalf("unexpected keys order: %v", keys) | ||
} | ||
} | ||
|
||
func TestKeys(t *testing.T) { | ||
m := map[string]int{ | ||
"foo": 1, | ||
"bar": 10, | ||
"aaaaa": 11, | ||
} | ||
|
||
keys := Keys(m) | ||
for _, s := range []string{"aaaaa", "bar", "foo"} { | ||
if !slices.Contains(keys, s) { | ||
t.Fatalf("unexpected keys: %v", keys) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ use ( | |
./errors | ||
./exp/golden | ||
./exp/higherorder | ||
./exp/maps | ||
./exp/open | ||
./exp/ordered | ||
./exp/slice | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters