Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -281,5 +281,6 @@ Josh Kelley <[email protected]>
Richard Taylor <[email protected]>
NilsDietrich <[email protected]>
anslem chibuike <[email protected]>
Ayomide Bamigbade <[email protected]>

# Generated by tools/update-authors.js
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fix: #3578 interpret empty true-expr of conditional as error (#3581).
Thanks @gwhitney.
- Fix: #3597 added nullish type definitions (#3601). Thanks @Ayo1984.
- Docs: fix #3565, update Matrix documentation (#3591). Thanks @orelbn.

# 2025-11-05, 15.1.0
Expand Down
7 changes: 4 additions & 3 deletions src/function/set/setDistinct.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const createSetDistinct = /* #__PURE__ */ factory(name, dependencies, ({
/**
* Collect the distinct elements of a multiset.
* A multi-dimension array will be converted to a single-dimension array before the operation.
* The items of the returned array will be in order of first occurrence.
*
* Syntax:
*
Expand All @@ -22,19 +23,19 @@ export const createSetDistinct = /* #__PURE__ */ factory(name, dependencies, ({
* setMultiplicity
*
* @param {Array | Matrix} a A multiset
* @return {Array | Matrix} A set containing the distinc elements of the multiset
* @return {Array | Matrix} A set containing the distinct elements of the multiset
*/
return typed(name, {
'Array | Matrix': function (a) {
let result
if (subset(size(a), new Index(0)) === 0) { // if empty, return empty
result = []
} else {
const b = flatten(Array.isArray(a) ? a : a.toArray()).sort(compareNatural)
const b = flatten(Array.isArray(a) ? a : a.toArray())
result = []
result.push(b[0])
for (let i = 1; i < b.length; i++) {
if (compareNatural(b[i], b[i - 1]) !== 0) {
if (!result.some(item => compareNatural(b[i], item) === 0)) {
result.push(b[i])
}
}
Expand Down
13 changes: 9 additions & 4 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
create,
divideDependencies,
EvalFunction,
evaluate,
factory,
formatDependencies,
Fraction,
Expand All @@ -23,6 +24,7 @@ import {
Help,
Index,
IndexNode,
isResultSet,
isSymbolNode,
LUDecomposition,
MapLike,
Expand All @@ -38,6 +40,7 @@ import {
MathType,
Matrix,
Node,
nullishDependencies,
ObjectNode,
OperatorNode,
OperatorNodeFn,
Expand All @@ -50,8 +53,6 @@ import {
SLUDecomposition,
SymbolNode,
Unit,
evaluate,
isResultSet,
UnitPrefix
} from 'mathjs'

Expand Down Expand Up @@ -2647,12 +2648,13 @@ Factory Test
}

// Create just the functions we need
const { fraction, add, divide, format } = create(
const { fraction, add, divide, format, nullish } = create(
{
fractionDependencies,
addDependencies,
divideDependencies,
formatDependencies
formatDependencies,
nullishDependencies
},
config
)
Expand All @@ -2667,6 +2669,9 @@ Factory Test
assert.strictEqual(format(255, { notation: 'bin' }), '0b11111111')
assert.strictEqual(format(255, { notation: 'hex' }), '0xff')
assert.strictEqual(format(255, { notation: 'oct' }), '0o377')
assert.strictEqual(nullish(null, 42), 42)
assert.strictEqual(nullish(undefined, 'foo'), 'foo')
assert.strictEqual(nullish(0, 42), 0)
}

/**
Expand Down
7 changes: 6 additions & 1 deletion test/unit-tests/function/set/setDistinct.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ describe('setDistinct', function () {
assert.deepStrictEqual(math.setDistinct([]), [])
})

it('should return the elements of a set in insertion order', function () {
assert.deepStrictEqual(math.setDistinct([3, 1, 2, 1, 2]), [3, 1, 2])
assert.deepStrictEqual(math.setDistinct(['h', 'e', 'l', 'l', 'o']), ['h', 'e', 'l', 'o'])
})

it('should return the distinct elements of a multiset', function () {
assert.deepStrictEqual(math.setDistinct([1, 1, 2, 2]), [1, 2])
assert.deepStrictEqual(math.setDistinct([1, 2, 1, 2]), [1, 2])
assert.deepStrictEqual(math.setDistinct([1, 2, math.complex(3, 3), 2, math.complex(3, 3)]), [math.complex(3, 3), 1, 2])
assert.deepStrictEqual(math.setDistinct([1, 2, math.complex(3, 3), 2, math.complex(3, 3)]), [1, 2, math.complex(3, 3)])
})

it('should return the same type of output as the inputs', function () {
Expand Down
24 changes: 24 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,18 @@ export interface MathJsInstance extends MathJsFactory {
x: number | BigNumber | bigint | Complex | Unit | MathCollection
): boolean | MathCollection

/**
* Nullish coalescing operator (??). Returns the right-hand side operand
* when the left-hand side operand is null or undefined, and otherwise
* returns the left-hand side operand. For matrices, the function is
* evaluated element wise.
* @param x First value to check
* @param y Fallback value
* @returns Returns y when x is null or undefined, otherwise returns x
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
nullish(x: any, y: any): any

/**
* Logical or. Test if at least one value is defined with a
* nonzero/nonempty value. For matrices, the function is evaluated
Expand Down Expand Up @@ -4081,6 +4093,7 @@ export const {
// logical dependencies
andDependencies,
notDependencies,
nullishDependencies,
orDependencies,
xorDependencies,

Expand Down Expand Up @@ -5988,6 +6001,16 @@ export interface MathJsChain<TValue> {
>
): MathJsChain<boolean | MathCollection>

/**
* Nullish coalescing operator (??). Returns the right-hand side operand
* when the left-hand side operand is null or undefined, and otherwise
* returns the left-hand side operand. For matrices, the function is
* evaluated element wise.
* @param y Fallback value
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
nullish(this: MathJsChain<any>, y: any): MathJsChain<any>

/**
* Logical or. Test if at least one value is defined with a
* nonzero/nonempty value. For matrices, the function is evaluated
Expand Down Expand Up @@ -7610,6 +7633,7 @@ export const {
// logical
and,
not,
nullish,
or,
xor,

Expand Down