-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseparate.go
More file actions
32 lines (26 loc) · 778 Bytes
/
separate.go
File metadata and controls
32 lines (26 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package nef
import (
"io/fs"
"github.com/snivilised/nefilim/internal/third/lo"
)
// Separate splits directory entries into files and folders (directories) by IsDir.
func Separate(entries []fs.DirEntry) (files, folders []fs.DirEntry) {
grouped := lo.GroupBy(entries, func(entry fs.DirEntry) bool {
return entry.IsDir()
})
const (
asFile = false
asFolder = true
)
// in-case lo.GroupBy has returned a nil for a particular grouping,
// we make sure we at least have an empty slice instead of allowing
// nil to be returned to represent an empty result set.
//
files = lo.Ternary(grouped[asFile] == nil,
[]fs.DirEntry{}, grouped[asFile],
)
folders = lo.Ternary(grouped[asFolder] == nil,
[]fs.DirEntry{}, grouped[asFolder],
)
return files, folders
}