feat: Add vector clone
This commit is contained in:
parent
7d150e2860
commit
f002310581
4 changed files with 43 additions and 0 deletions
24
src/vector/__test__/utils.spec.js
Normal file
24
src/vector/__test__/utils.spec.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
import assert from 'assert'
|
||||
import math from '#nanomath'
|
||||
|
||||
describe('Vector utility functions', () => {
|
||||
it('can clone a vector', () => {
|
||||
const subj1 = [3, 4]
|
||||
const clone1 = math.clone(subj1)
|
||||
assert.deepStrictEqual(clone1, subj1)
|
||||
assert(clone1 !== subj1)
|
||||
const subj2 = [3, false]
|
||||
const clone2 = math.clone(subj2)
|
||||
assert.deepStrictEqual(clone2, subj2)
|
||||
assert(clone2 !== subj2)
|
||||
const subj3 = [[3, 4], [2, 5]]
|
||||
const clone3 = math.clone(subj3)
|
||||
assert.deepStrictEqual(clone3, subj3)
|
||||
assert(clone3 !== subj3)
|
||||
assert(clone3[0] !== subj3[0])
|
||||
assert(clone3[1] !== subj3[1])
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
export * as typeDefinition from './Vector.js'
|
||||
export * as type from './type.js'
|
||||
export * as utilities from './utils.js'
|
||||
|
||||
|
|
15
src/vector/helpers.js
Normal file
15
src/vector/helpers.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
import {Vector} from './Vector.js'
|
||||
import {NotAType, Returns} from '#core/Type.js'
|
||||
import {match} from '#core/TypePatterns.js'
|
||||
|
||||
export const promoteUnary = name => match(Vector, (math, V, strategy) => {
|
||||
if (V.Component === NotAType) {
|
||||
// have to resolve element by element :-(
|
||||
return Returns(V, v => v.map(
|
||||
elt => math.resolve(name, math.typeOf(elt), strategy)(elt)))
|
||||
}
|
||||
const compOp = math.resolve(name, V.Component, strategy)
|
||||
return Returns(Vector(compOp.returns), v => v.map(elt => compOp(elt)))
|
||||
})
|
||||
|
||||
|
3
src/vector/utils.js
Normal file
3
src/vector/utils.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
import {promoteUnary} from './helpers.js'
|
||||
|
||||
export const clone = promoteUnary('clone')
|
Loading…
Add table
Add a link
Reference in a new issue