MCPcopy Create free account
hub / github.com/denoland/std / getValuesFromItem

Function getValuesFromItem

csv/stringify.ts:177–217  ·  view source on GitHub ↗

* Returns an array of values from an object using the property accessors * (and optional transform function) in each column

(
  item: DataItem,
  normalizedColumns: readonly NormalizedColumn[],
)

Source from the content-addressed store, hash-verified

175 * (and optional transform function) in each column
176 */
177function getValuesFromItem(
178 item: DataItem,
179 normalizedColumns: readonly NormalizedColumn[],
180): unknown[] {
181 const values: unknown[] = [];
182
183 if (normalizedColumns.length) {
184 for (const column of normalizedColumns) {
185 let value: unknown = item;
186
187 for (const prop of column.prop) {
188 if (typeof value !== "object" || value === null) {
189 continue;
190 }
191 if (Array.isArray(value)) {
192 if (typeof prop === "number") value = value[prop];
193 else {
194 throw new TypeError(
195 'Property accessor is not of type "number"',
196 );
197 }
198 } // I think this assertion is safe. Confirm?
199 else value = (value as Record<string, unknown>)[prop];
200 }
201
202 values.push(value);
203 }
204 } else {
205 if (Array.isArray(item)) {
206 values.push(...item);
207 } else if (typeof item === "object") {
208 throw new TypeError(
209 "No property accessor function was provided for object",
210 );
211 } else {
212 values.push(item);
213 }
214 }
215
216 return values;
217}
218
219/**
220 * Converts an array of objects into a CSV string.

Callers 1

stringifyFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected