From d27ed24589f6c8be1099bfdc2e35410408752d27 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Tue, 28 Dec 2021 19:26:42 -0400 Subject: [PATCH] feat: map --- .gitignore | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ .golangci.yml | 42 ++++++++++++++++++++++++++++++++++++ Dockerfile | 11 ++++++++++ Makefile | 5 +++++ README.md | 4 ++++ go.mod | 11 ++++++++++ go.sum | 15 +++++++++++++ map.go | 9 ++++++++ map_test.go | 18 ++++++++++++++++ 9 files changed, 174 insertions(+) create mode 100644 .gitignore create mode 100644 .golangci.yml create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 map.go create mode 100644 map_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..febd2c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,59 @@ +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace +.history/ +.idea/ +cmake-build-*/ +*.iws +out/ +.idea_modules/ +atlassian-ide-plugin.xml +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +*~ +.fuse_hidden* +.directory +.Trash-* +.nfs* +*.exe +*.exe~ +*.dll +*.so +*.dylib +*.test +*.out +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db +*.stackdump +[Dd]esktop.ini +$RECYCLE.BIN/ +*.cab +*.msi +*.msix +*.msm +*.msp +*.lnk +.DS_Store +.AppleDouble +.LSOverride +Icon +._* +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..0cdfa63 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,42 @@ +skip-dirs-use-default: true + +run: + timeout: 5m + +linters: + enable: + - bodyclose + - deadcode + - depguard + - dogsled + - errcheck + - errorlint + - exportloopref + - gocritic + - gocyclo + - gofmt + - goimports + - goprintffuncname + - gosimple + - gosec + - govet + - ineffassign + - misspell + - noctx + - nolintlint + - prealloc + - rowserrcheck + - staticcheck + - structcheck + - stylecheck + - typecheck + - unconvert + - unparam + - unused + - varcheck + - whitespace + fast: true + +linters-settings: + goimports: + local-prefixes: github.com/rjNemo/underscore \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..448f9c7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:1.18beta1-bullseye + +WORKDIR /app + +COPY go.* ./ +RUN go mod download + +COPY *.go ./ + +#RUN go test -json ./... -count=1 -cover | gotestfmt +RUN go test ./... \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..be61efe --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +lint: + golangci-lint run --fix + +test: + go test -json ./... -count=1 -cover | gotestfmt \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..31978f0 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Underscore + +A port from the `underscore.js` library based on generics brought by `go1.18`. + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..0066efa --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/rjNemo/underscore + +go 1.18 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/stretchr/objx v0.3.0 // indirect + github.com/stretchr/testify v1.7.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8d71661 --- /dev/null +++ b/go.sum @@ -0,0 +1,15 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As= +github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/map.go b/map.go new file mode 100644 index 0000000..3797dd8 --- /dev/null +++ b/map.go @@ -0,0 +1,9 @@ +package underscore + +func Map[T, P any](values []T, predicate func(T) P) []P { + res := make([]P, 0, len(values)) + for _, v := range values { + res = append(res, predicate(v)) + } + return res +} diff --git a/map_test.go b/map_test.go new file mode 100644 index 0000000..e939357 --- /dev/null +++ b/map_test.go @@ -0,0 +1,18 @@ +package underscore_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + u "github.com/rjNemo/underscore" +) + +func TestMap(t *testing.T) { + nums := []int{1, 2, 3} + f := func(n int) int { + return n * n + } + want := []int{1, 4, 9} + assert.Equal(t, want, u.Map(nums, f)) +}