{"id":23443805,"url":"https://github.com/matthewoestreich/jslice","last_synced_at":"2026-03-14T00:06:54.285Z","repository":{"id":252888795,"uuid":"841802160","full_name":"matthewoestreich/jslice","owner":"matthewoestreich","description":"Generic, JS-like array methods for Go slices","archived":false,"fork":false,"pushed_at":"2024-10-10T16:38:55.000Z","size":38,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-15T14:32:57.575Z","etag":null,"topics":["array","array-methods","go","golang","golang-generics","javascript-array-methods","slice","slice-methods"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/matthewoestreich.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-13T04:28:24.000Z","updated_at":"2024-10-10T16:36:38.000Z","dependencies_parsed_at":"2024-11-21T06:18:57.776Z","dependency_job_id":null,"html_url":"https://github.com/matthewoestreich/jslice","commit_stats":null,"previous_names":["oze4/jslice","matthewoestreich/jslice"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matthewoestreich%2Fjslice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matthewoestreich%2Fjslice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matthewoestreich%2Fjslice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matthewoestreich%2Fjslice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matthewoestreich","download_url":"https://codeload.github.com/matthewoestreich/jslice/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248125402,"owners_count":21051762,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","array-methods","go","golang","golang-generics","javascript-array-methods","slice","slice-methods"],"created_at":"2024-12-23T18:25:14.365Z","updated_at":"2025-12-15T18:43:36.170Z","avatar_url":"https://github.com/matthewoestreich.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jslice\n\n`jslice`, pronounced JS-slice (jay-es-slice), provides generic, JavaScript-like [array methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#instance_methods) for Go slices.\n\nTo check coverage run: `make test-coverage`\n\n## Filter\n\nFilters an array.\n\n```go\ns := []int{1,1,1,9,9,9}\n\nr := jslice.Filter(s, func(index int, element int) bool {\n  return element \u003e 5\n})\n// r == []int{9,9,9}\n```\n\n```go\ntype TestResult struct {\n  Result string \n}\n\ns := []TestResult{{Result: \"Fail\", {Result: \"Pass\"}, {Result: \"In-progress\"}}}\n\nfails := jslice.Filter(s, func(i int, e TestResult) bool {\n  return e.Result == \"Fail\"\n})\n// fails == []TestResult{{Result: \"Fail\"}}\n```\n\n## Map\n\nMaps over an array and returns a new array.\n\n```go\ntype In struct {\n  Foo int\n}\ntype Out struct {\n  Bar string\n}\n\ns := []In{{Foo: 1}, {Foo: 2}, {Foo: 3}, {Foo: 4}}\n\nr := jslice.Map(s, func(i int, e In) Out {\n  return Out{Bar: strconv.Itoa(e.Foo)}\n})\n// r == []Out{{Bar: \"1\"}, {Bar: \"2\"}, {Bar: \"3\"}, {Bar: \"4\"}}\n```\n\n## Reduce\n\nExecutes the provided reducer function and stores the result in an accumulator, which is returned at the time of completion.\n\n```go\ns := []int{54,43,32,21,10,9}\n\nreducer := func(acc int, currEl int, currIdx int, originalSlice []int) int {\n  return acc + currEl\n}\n\nsum := jslice.Reduce(s, reducer, 0)\n// sum == 169\n```\n\n```go\ntype Number struct {\n  Value int\n}\n\ns := []int{1,2,3}\n\nreducer := func(acc []Number, e int, i int, og []int) []Number {\n  num := Number{Value: e}\n  return append(acc, num)\n}\n\nr := jslice.Reduce(s, reducer, []Number{})\n// r == []Number{{Value: 1}, {Value: 2}, {Value: 3}}\n```\n\n## ForEach\n\nIterates over a slice calling the provided function on each element. Note: `ForEach` does not return anything.\n\n```go\ns := []int{1,2,3}\n\njslice.ForEach(s, func(i int, e int) {\n  fmt.Printf(\"Element '%d' is at index '%d'.\\n\", i, e)\n})\n```\n\n## Push\n\nAppends an element to the end of a slice. `Push` modifies the original slice.\n\n```go\ns := []int{1,2,3}\ni := 4\njslice.Push(\u0026s, i)\n// s == []int{1,2,3,4}\n```\n\n## Pop\n\nRemoves element from end of slice and returns the element that was removed. `Pop` modifies the original slice.\n\n```go\ns := []int{1,2,3}\nitem := jslice.Pop(\u0026s)\n// s == []int{1,2}\n// item == 3\n```\n\n## Shift\n\nRemoves first element (index 0) from slice and returns the removed element. `Shift` modifies the orignal slice.\n\n```go\ns := []int{1,2,3,4}\ni := jslice.Shift(\u0026s)\n// s == []int{2,3,4}\n// i == 1\n```\n\n## Some\n\nTests that at least one element in the slice matches provided condition.\n\n```go\ns := []int{1,1,2,1,1}\n\nr := jslice.Some(s, func(i int, e int) bool {\n  return e == 2\n})\n// r == true\n```\n\n## Every\n\nTests that every element in slice meets provided condition. Returns false if at least one element does not meet condition.\n\n```go\ntype Shipment struct {\n  Source string\n}\n\ns := []Shipment{{Source: \"New York\"}, {Source: \"New York\"}, {Source: \"New York\"}}\n\nr := jslice.Every(s, func(i int, e Shipment) bool {\n  return e.Source == \"New York\"\n})\n// r == true\n```\n\n## Slice\n\nReturns a copy of a portion of a slice. We return a slice from `start` up to, but not including, `end`. If `end \u003e len(slice)` we default to `end = len(slice)`. \n\n```go\ns := []int{1,2,3,4,5}\nstart := 0\nend := 3\nr := jslice.Slice(s, start, end)\n// r == []int{1,2,3}\n```\n\n## Splice\n\nChanges the contents of a slice by removing or replacing existing elements and/or adding new elements.\n\n- If `deleteCount` and `replacementElements` both equal `0`, we just return the original slice without modifying anything.\n- If `start` is greater than or equal to the length of the slice, no elements will be deleted, but the method will behave as an adding function.\n\n\u003cb\u003eRemove `0` elements before index `2` and insert \"`earth`\" and \"`mars`\"\u003c/b\u003e\n```go\ns := []string{\"mercury\", \"venus\", \"jupiter\", \"saturn\"}\njslice.Splice(\u0026s, 2, 0, \"earth\", \"mars\")\n// s == []string{\"mercury\", \"venus\", \"earth\", \"mars\", \"jupiter\", \"saturn\"}\n```\n\n**Remove `0` elements at index `0` and insert \"`earth`\" and \"`mars`\".**\n\n```go\ns := []string{\"mercury\", \"venus\", \"jupiter\", \"saturn\"}\njslice.Splice(\u0026s, 0, 0, \"earth\", \"mars\")\n// s == []string{\"earth\", \"mars\", \"mercury\", \"venus\", \"jupiter\", \"saturn\"}\n```\n\n**Remove `1` element at index `2`, and insert \"`earth`\" and \"`mars`\"**\n\n```go\ns := []string{\"mercury\", \"venus\", \"jupiter\", \"saturn\"}\njslice.Splice(\u0026s, 2, 1, \"earth\", \"mars\")\n// s == []string{\"mercury\", \"venus\", \"earth\", \"mars\", \"saturn\"}\n```\n\n**Remove `1` element at index `0` and insert \"`earth`\" and \"`mars`\"**\n\n```go\ns := []string{\"mercury\", \"venus\", \"jupiter\", \"saturn\"}\njslice.Splice(\u0026s, 0, 1, \"earth\", \"mars\")\n// s == []string{\"earth\", \"mars\", \"venus\", \"jupiter\", \"saturn\"}\n```\n\n**Remove `3` elements starting at index `1` and insert nothing**\n\n```go\ns := []string{\"mercury\", \"venus\", \"jupiter\", \"saturn\"}\njslice.Splice(\u0026s, 1, 3)\n// s == []string{\"mercury\"}\n```\n\n**If `start` + `deleteCount` is greater than or equal to slice length, we modify `deleteCount` to equal the length of the slice - `start`**\n\n```go\ns := []string{\"mercury\", \"venus\", \"jupiter\", \"saturn\"}\njslice.Splice(\u0026s, 2, 100) // \u003c- 100 greater than slice length\n// s == []string{\"mercury\", \"venus\"}\n```\n\n**If `start` is greater than or equal to the length of the slice, no elements are removed, but the method is treated as an add function**\n\n```go\ns := []string{\"mercury\", \"venus\", \"jupiter\", \"saturn\"}\n// Even though `deleteCount` == 1, nothing will be\n// deleted because `start` \u003e= length of slice.\njslice.Splice(\u0026s, 100, 1, \"earth\", \"mars\") \n// s == []string{\"mercury\", \"venus\", \"jupiter\", \"saturn\", \"earth\", \"mars\"}\n```\n\n**Splice last element by removing `1` element at index `3` and inserting \"`earth`\" and \"`mars`\"**\n\n```go\ns := []string{\"mercury\", \"venus\", \"jupiter\", \"saturn\"}\njslice.Splice(\u0026s, 3, 1, \"earth\", \"mars\")\n// s == []string{\"mercury\", \"venus\", \"jupiter\", \"earth\", \"mars\"}\n```\n\n## Reverse\n\nModifies a slice in-place by reversing it's elements. If you do not want to modify the slice in-place, use `ToReversed` method.\n\n```go\ns := []int{1,2,3,4,5}\njslice.Reverse(\u0026s)\n// s == []int{5,4,3,2,1}\n```\n\n## ToReversed\n\nReturns a copy of a slice in reversed order.\n\n```go\ns := []string{\"foo\", \"bar\", \"baz\"}\nr := jslice.ToReversed(s)\n// s == []string{\"foo\", \"bar\", \"baz\"}\n// r == []string{\"baz\", \"bar\", \"foo\"}\n```\n\n## Unshift\n\nAdds an element to the front of a slice.\n\n```go\ns := []int{2,3,4,5}\njslice.Unshift(\u0026s, 1)\n// s == []int{1,2,3,4,5}\n```\n\n## At\n\nAt takes an integer value and returns the item at that index, allowing for **positive** _and_ **negative** integers. Negative integers\ncount back from the last item in the array. \n\n**NOTE**: If the provided index is negative, and it's absolute value is greater than the length of the array, we return the first item (index 0) in the array.\n\n```go\ns := []int{1,2,3,4,5}\nr := jslice.At(s, -3)\n// r == 3\n```\n\n\n\n\n\u003cbr /\u003e\n\u003cbr /\u003e\n\u003cbr /\u003e\n\u003cbr /\u003e\n\n------\n\n\u003cfooter\u003e\n\u003csmall\u003ematt oestreich\u003c/small\u003e\n\u003c/footer\u003e\n\n----","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatthewoestreich%2Fjslice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatthewoestreich%2Fjslice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatthewoestreich%2Fjslice/lists"}