js

package module
v0.0.0-...-858d54b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2022 License: MIT Imports: 1 Imported by: 2

README

go-js

push

Deprecated in favor of go-fn which removes JavaScript specific stuff that doesn't work as nicely in Go without optional paramter handling.

Go module for functions that mimic useful JavaScript functions using Go 1.18's Generics

Install

go get github.com/frantjc/go-js

Usage

See examples.

import (
	"fmt"

	"github.com/frantjc/go-js"
)
// ...
	array := []int{1, 2, 3, 4}
	mappable := js.MappableArray[int, string](array)
	some := mappable.Map(func(a, _ int, _ []int) string {
		return fmt.Sprint(a)
	}).Some(func(b string, _ int, _ []string) bool {
		return b == "1"
	})

	fmt.Println(some)
	// true
// ...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Coalesce

func Coalesce[T comparable](ts ...T) T

Coalesce is meant to represent the '||' operator e.g.

const myVar = otherVar || 'default';

It returns the first non-zero-valued instance of T that it finds, or the zero value of T if it finds none.

func Every

func Every[T any](in []T, f func(T, int, []T) bool) bool

Every tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

func Filter

func Filter[T any](in []T, f func(T, int, []T) bool) []T

Filter creates a new array with all elements that pass the test implemented by the provided function.

func Find

func Find[T any](in []T, f func(T, int, []T) bool) T

Find returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, the zero value of T is returned.

func FindIndex

func FindIndex[T any](in []T, f func(T, int, []T) bool) int

FindIndex returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test

func ForEach

func ForEach[T any](in []T, f func(T, int, []T))

ForEach executes a provided function once for each array element

func Includes

func Includes[T comparable](in []T, a T) bool

Includes determines whether an array includes a certain value among its entries, returning true or false as appropriate.

func IndexOf

func IndexOf[T comparable](in []T, a T) int

IndexOf returns the first index at which a given element can be found in the array, or -1 if it is not present.

func Join

func Join[T fmt.Stringer](in []T, separator string) string

Join creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

func LastIndexOf

func LastIndexOf[T comparable](in []T, a T, from int) int

LastIndexOf returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

func Map

func Map[T1, T2 any](in []T1, f func(T1, int, []T1) T2) []T2

Map creates a new array populated with the results of calling a provided function on every element in the calling array.

func Reduce

func Reduce[T1, T2 any](in []T1, f func(T2, T1, int, []T1) T2, initial T2) T2

Reduce executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

The first time that the callback is run there is no "return value of the previous calculation". If supplied, an initial value may be used in its place. Otherwise the array element at index 0 is used as the initial value and iteration starts from the next element (index 1 instead of index 0).

func ReduceRight

func ReduceRight[T1, T2 any](in []T1, f func(T2, T1, int, []T1) T2, initial T2) T2

ReduceRight applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.

func Reverse

func Reverse[T any](in []T) []T

Reverse creates a new array by reversing the given array. The first array element becomes the last, and the last array element becomes the first.

func Slice

func Slice[T any](in []T, start, end int) []T

Slice returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

If start<0, it is treated as distance from the end of the array. If end<=0, it is treated as distance from the end of the array.

func Some

func Some[T any](in []T, f func(T, int, []T) bool) bool

Some tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

func Sort

func Sort[T comparable](in []T, f func(a, b T) int) []T

Sort creates a new array by sorting the elements of the given array and returns the sorted array. The sort order is ascending.

func Ternary

func Ternary[T any](condition bool, a, b T) T

Ternary returns the first T if the conditional evaluates to true, otherwise it returns the second T

func Unique

func Unique[T comparable](in []T) []T

Unique creates a new array with only unique elements from the given array.

Types

type AnyArray

type AnyArray[T any] []T

AnyArray is an array of any and has methods that mimic a subset of the JavaScript array functions.

func (AnyArray[T]) Every

func (a AnyArray[T]) Every(f func(T, int, []T) bool) bool

Every tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

func (AnyArray[T]) Filter

func (a AnyArray[T]) Filter(f func(T, int, []T) bool) AnyArray[T]

Filter creates a new array with all elements that pass the test implemented by the provided function.

func (AnyArray[T]) Find

func (a AnyArray[T]) Find(f func(T, int, []T) bool) T

Find returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, the zero value of T is returned.

func (AnyArray[T]) FindIndex

func (a AnyArray[T]) FindIndex(f func(T, int, []T) bool) int

FindIndex returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test

func (AnyArray[T]) ForEach

func (a AnyArray[T]) ForEach(f func(T, int, []T))

ForEach executes a provided function once for each array element

func (AnyArray[T]) Reverse

func (a AnyArray[T]) Reverse() AnyArray[T]

Reverse creates a new array by reversing the given array. The first array element becomes the last, and the last array element becomes the first.

func (AnyArray[T]) Slice

func (a AnyArray[T]) Slice(start, end int) AnyArray[T]

Slice returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

If start<0, it is treated as distance from the end of the array. If end<=0, it is treated as distance from the end of the array.

func (AnyArray[T]) Some

func (a AnyArray[T]) Some(f func(T, int, []T) bool) bool

Some tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

type ComparableArray

type ComparableArray[T comparable] AnyArray[T]

ComparableArray is an array of fmt.Comparable and has methods that mimic a subset of the JavaScript array functions that are unique to comparables.

func (ComparableArray[T]) Includes

func (a ComparableArray[T]) Includes(b T) bool

Includes determines whether an array includes a certain value among its entries, returning true or false as appropriate.

func (ComparableArray[T]) IndexOf

func (a ComparableArray[T]) IndexOf(b T) int

IndexOf returns the first index at which a given element can be found in the array, or -1 if it is not present.

func (ComparableArray[T]) LastIndexOf

func (a ComparableArray[T]) LastIndexOf(b T, from int) int

LastIndexOf returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.

func (ComparableArray[T]) Unique

func (a ComparableArray[T]) Unique() ComparableArray[T]

Unique creates a new array with only unique elements from the given array.

type MappableArray

type MappableArray[T1, T2 any] AnyArray[T1]

MappableArray is an array of any and has methods that mimic the JavaScript array map function.

T1 represents the type of the array, while T2 represents the type of the array intended to be mapped to

func (MappableArray[T1, T2]) Map

func (a MappableArray[T1, T2]) Map(f func(T1, int, []T1) T2) AnyArray[T2]

Map creates a new array populated with the results of calling a provided function on every element in the calling array.

type ReducibleArray

type ReducibleArray[T1, T2 any] AnyArray[T1]

ReducibleArray is an array of any and has methods that mimic the JavaScript array reduce functions.

T1 represents the type of the array, while T2 represents the type of the accumulator intended to be used when reducing.

func (ReducibleArray[T1, T2]) Reduce

func (a ReducibleArray[T1, T2]) Reduce(f func(T2, T1, int, []T1) T2, initial T2) T2

Reduce executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

The first time that the callback is run there is no "return value of the previous calculation". If supplied, an initial value may be used in its place. Otherwise the array element at index 0 is used as the initial value and iteration starts from the next element (index 1 instead of index 0).

func (ReducibleArray[T1, T2]) ReduceRight

func (a ReducibleArray[T1, T2]) ReduceRight(f func(T2, T1, int, []T1) T2, initial T2) T2

ReduceRight applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.

type StringerArray

type StringerArray[T fmt.Stringer] AnyArray[T]

StringerArray is an array of fmt.Stringer and has methods that mimic a subset of the JavaScript array functions that are unique to fmt.Stringers.

func (StringerArray[T]) Join

func (a StringerArray[T]) Join(separator string) string

Join creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL