Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: darvaza-proxy/core
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.16.1
Choose a base ref
...
head repository: darvaza-proxy/core
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.16.2
Choose a head ref
  • 4 commits
  • 3 files changed
  • 3 contributors

Commits on Mar 11, 2025

  1. chore(deps): update dependency node to v22

    renovate[bot] authored Mar 11, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    4f41f4f View commit details

Commits on Mar 23, 2025

  1. maps: introduce SortedValues() helper

    also providing conditional variants, with and without preallocation.
    * SortedValues() - unconditional
    * SortedValuesCond() - conditional with preallocation
    * SortedValuesUnlikelyCond() - conditional without preallocation
    
    Signed-off-by: Alejandro Mery <amery@apptly.co>
    amery committed Mar 23, 2025

    Partially verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
    Copy the full SHA
    c1b7d70 View commit details
  2. Merge pull request #100 (deps)

    chore(deps): update dependency node to v22
    amery authored Mar 23, 2025

    Unverified

    This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
    Copy the full SHA
    33e278d View commit details

Commits on Mar 24, 2025

  1. Merge pull request #101

    maps: introduce SortedValues() helper
    karasz authored Mar 24, 2025
    Copy the full SHA
    4f1ba9b View commit details
Showing with 53 additions and 4 deletions.
  1. +1 −1 .github/workflows/renovate.yml
  2. +10 −3 README.md
  3. +42 −0 maps.go
2 changes: 1 addition & 1 deletion .github/workflows/renovate.yml
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18'
node-version: '22'

- name: Run renovate-config-validator
run: npx --yes --package renovate -- renovate-config-validator --strict
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -47,7 +47,6 @@ should be on a subdirectory, it shouldn't be here.
* ListForEach/ListForEachElement
* ListForEachBackward/ListForEachBackwardElement
* ListCopy/ListCopyFn
* MapContains
* MapListContains/MapListContainsFn
* MapListForEach/MapListForEachElement
* MapListInsert/MapListAppend
@@ -56,8 +55,16 @@ should be on a subdirectory, it shouldn't be here.
* MapListCopy/MapListCopyFn
* MapAllListContains/MapAllListContainsFn
* MapAllListForEach/MapAllListForEachElement
* MapValue
* Keys()/SortedKeys()

### Maps

* `MapContains()` checks if a map contains a key. Useful for switch/case tests.
* `MapValue()` returns the value for a key, or a given fallback value if the key is not present.
* `Keys()` returns a slice of the keys in the map.
* `SortedKeys()` returns a sorted slice of the keys in the map.
* `SortedValues()` returns a slice of the values in the map, sorted by key.
* `SortedValuesCond()` returns a slice of the values in the map, sorted by key, and optionally filtered by a condition function.
* `SortedValuesUnlikelyCond()` is like `SortedValuesCond()` but it doesn't reallocate the slice.

## Errors

42 changes: 42 additions & 0 deletions maps.go
Original file line number Diff line number Diff line change
@@ -29,6 +29,48 @@ func SortedKeys[K Ordered, T any](m map[K]T) []K {
return keys
}

// SortedValues returns a slice of values from the map, sorted by their keys
func SortedValues[K Ordered, T any](m map[K]T) []T {
var out []T
if l := len(m); l > 0 {
out = make([]T, 0, l)
out = doSortedValues(out, m, nil)
}
return out
}

// SortedValuesCond returns a slice of values from the map, sorted by their keys,
// filtered by the optional predicate function fn, preallocated to the size of the map.
func SortedValuesCond[K Ordered, T any](m map[K]T, fn func(T) bool) []T {
var out []T
if l := len(m); l > 0 {
out = make([]T, 0, l)
out = doSortedValues(out, m, fn)
}
return out
}

// SortedValuesUnlikelyCond returns a slice of values from the map, sorted by their keys,
// filtered by the optional predicate function fn, without preallocation.
func SortedValuesUnlikelyCond[K Ordered, T any](m map[K]T, fn func(T) bool) []T {
var out []T
if len(m) > 0 {
out = doSortedValues(nil, m, fn)
}
return out
}

func doSortedValues[K Ordered, T any](out []T, m map[K]T, fn func(T) bool) []T {
for _, k := range SortedKeys(m) {
if v, ok := m[k]; ok {
if fn == nil || fn(v) {
out = append(out, v)
}
}
}
return out
}

// MapValue returns a value of an entry or a default if
// not found
func MapValue[K comparable, V any](m map[K]V, key K, def V) (V, bool) {