29 lines
1.2 KiB
TypeScript
29 lines
1.2 KiB
TypeScript
import { sameValue, untag } from '../../../model/Properties';
|
||
import type { NodeDef } from '../../NodeRegistry';
|
||
import { asList, asValueItem, isEntity } from '../../values';
|
||
import { any, list, num } from '../shared';
|
||
|
||
export const IndexOf: NodeDef = {
|
||
type: 'IndexOf',
|
||
label: 'Index of',
|
||
category: 'data',
|
||
description: 'Where a value sits in a list, or -1 if it is not in there – how "is this row full" is asked.',
|
||
inputs: [list('list'), any('value')],
|
||
outputs: [num('index')],
|
||
params: [],
|
||
// Compared by content, not by identity: two equal structs are never the same object. A color item
|
||
// is compared as its number, or searching a list of colors with one would never find it.
|
||
run: (_ctx, inputs) => {
|
||
const wanted = asValueItem(inputs.value);
|
||
// Two objects are the same one or they are not – there is no content to compare (#157).
|
||
if (isEntity(wanted))
|
||
return { outputs: { index: asList(inputs.list).findIndex((item) => item === wanted) } };
|
||
const value = untag(wanted);
|
||
return {
|
||
outputs: {
|
||
index: asList(inputs.list).findIndex((item) => !isEntity(item) && sameValue(untag(item), value)),
|
||
},
|
||
};
|
||
},
|
||
};
|