|
18 | 18 | * site reads them from its own source). |
19 | 19 | */ |
20 | 20 |
|
| 21 | +/** The four optogenetics field VALUES (each call site reads them from its own source). */ |
| 22 | +export interface OptoFields { |
| 23 | + /** The excitation-source list. */ |
| 24 | + opto_excitation_source?: unknown; |
| 25 | + /** The optical-fiber list. */ |
| 26 | + optical_fiber?: unknown; |
| 27 | + /** The virus-injection list. */ |
| 28 | + virus_injection?: unknown; |
| 29 | + /** The stimulation-software string. */ |
| 30 | + optogenetic_stimulation_software?: unknown; |
| 31 | +} |
| 32 | + |
| 33 | +/** Each optogenetics field's presence boolean plus the total count of present fields. */ |
| 34 | +export interface OptoFieldsPresence { |
| 35 | + opto_excitation_source: boolean; |
| 36 | + optical_fiber: boolean; |
| 37 | + virus_injection: boolean; |
| 38 | + optogenetic_stimulation_software: boolean; |
| 39 | + count: number; |
| 40 | +} |
| 41 | + |
21 | 42 | /** |
22 | 43 | * A list opto field counts as present only when it's a non-empty array (corrupt non-array → absent). |
23 | 44 | * @param value |
24 | 45 | */ |
25 | | -const isOptoListPresent = (value) => Array.isArray(value) && value.length > 0; |
| 46 | +const isOptoListPresent = (value: unknown): boolean => Array.isArray(value) && value.length > 0; |
26 | 47 |
|
27 | 48 | /** |
28 | 49 | * The software field counts as present only when it's a non-empty trimmed string. |
29 | 50 | * @param value |
30 | 51 | */ |
31 | | -const isOptoSoftwarePresent = (value) => typeof value === 'string' && value.trim() !== ''; |
| 52 | +const isOptoSoftwarePresent = (value: unknown): boolean => |
| 53 | + typeof value === 'string' && value.trim() !== ''; |
32 | 54 |
|
33 | 55 | /** |
34 | 56 | * Compute the per-field presence of the four optogenetics fields from their VALUES. |
35 | 57 | * |
36 | | - * @param {object} fields - The four field values (read from the caller's own source). |
37 | | - * @param {*} fields.opto_excitation_source - The excitation-source list. |
38 | | - * @param {*} fields.optical_fiber - The optical-fiber list. |
39 | | - * @param {*} fields.virus_injection - The virus-injection list. |
40 | | - * @param {*} fields.optogenetic_stimulation_software - The stimulation-software string. |
41 | | - * @returns {{ |
42 | | - * opto_excitation_source: boolean, |
43 | | - * optical_fiber: boolean, |
44 | | - * virus_injection: boolean, |
45 | | - * optogenetic_stimulation_software: boolean, |
46 | | - * count: number, |
47 | | - * }} Each field's presence boolean plus the total count of present fields. |
| 58 | + * @param fields - The four field values (read from the caller's own source). |
48 | 59 | */ |
49 | | -export function optoFieldsPresence(fields) { |
50 | | - const source = fields || {}; |
51 | | - const presence = { |
52 | | - opto_excitation_source: isOptoListPresent(source.opto_excitation_source), |
53 | | - optical_fiber: isOptoListPresent(source.optical_fiber), |
54 | | - virus_injection: isOptoListPresent(source.virus_injection), |
55 | | - optogenetic_stimulation_software: isOptoSoftwarePresent( |
56 | | - source.optogenetic_stimulation_software |
57 | | - ), |
| 60 | +export function optoFieldsPresence(fields: OptoFields | null | undefined): OptoFieldsPresence { |
| 61 | + const source: OptoFields = fields || {}; |
| 62 | + const opto_excitation_source = isOptoListPresent(source.opto_excitation_source); |
| 63 | + const optical_fiber = isOptoListPresent(source.optical_fiber); |
| 64 | + const virus_injection = isOptoListPresent(source.virus_injection); |
| 65 | + const optogenetic_stimulation_software = isOptoSoftwarePresent( |
| 66 | + source.optogenetic_stimulation_software |
| 67 | + ); |
| 68 | + return { |
| 69 | + opto_excitation_source, |
| 70 | + optical_fiber, |
| 71 | + virus_injection, |
| 72 | + optogenetic_stimulation_software, |
| 73 | + count: [ |
| 74 | + opto_excitation_source, |
| 75 | + optical_fiber, |
| 76 | + virus_injection, |
| 77 | + optogenetic_stimulation_software, |
| 78 | + ].filter(Boolean).length, |
58 | 79 | }; |
59 | | - presence.count = [ |
60 | | - presence.opto_excitation_source, |
61 | | - presence.optical_fiber, |
62 | | - presence.virus_injection, |
63 | | - presence.optogenetic_stimulation_software, |
64 | | - ].filter(Boolean).length; |
65 | | - return presence; |
66 | 80 | } |
0 commit comments