MCPcopy Index your code
hub / github.com/gavv/httpexpect / Transform

Method Transform

array.go:490–515  ·  view source on GitHub ↗

Transform runs the passed function on all the elements in the array and returns a new array without effeecting original array. Example: array := NewArray(t, []interface{}{"foo", "bar"}) transformedArray := array.Transform( func(index int, value interface{}) interface{} { return strings.ToUp

(fn func(index int, value interface{}) interface{})

Source from the content-addressed store, hash-verified

488// })
489// transformedArray.IsEqual([]interface{}{"FOO", "BAR"})
490func (a *Array) Transform(fn func(index int, value interface{}) interface{}) *Array {
491 opChain := a.chain.enter("Transform()")
492 defer opChain.leave()
493
494 if opChain.failed() {
495 return newArray(opChain, nil)
496 }
497
498 if fn == nil {
499 opChain.fail(AssertionFailure{
500 Type: AssertUsage,
501 Errors: []error{
502 errors.New("unexpected nil function argument"),
503 },
504 })
505 return newArray(opChain, nil)
506 }
507
508 transformedArray := []interface{}{}
509
510 for index, element := range a.value {
511 transformedArray = append(transformedArray, fn(index, element))
512 }
513
514 return newArray(opChain, transformedArray)
515}
516
517// Find accepts a function that returns a boolean, runs it over the array
518// elements, and returns the first element on which it returned true.

Callers 3

TestArray_TransformFunction · 0.95
TestArray_FailedChainFunction · 0.45
TestObject_FailedChainFunction · 0.45

Calls 5

newArrayFunction · 0.85
enterMethod · 0.80
leaveMethod · 0.80
failedMethod · 0.80
failMethod · 0.80

Tested by 3

TestArray_TransformFunction · 0.76
TestArray_FailedChainFunction · 0.36
TestObject_FailedChainFunction · 0.36