pebblebed
Version:
Simplified interactions with Google Datastore for NodeJS
17 lines (15 loc) • 477 B
text/typescript
import { TReturnOnly } from "../";
export default function<T>(resultArray: T[], pickType: TReturnOnly): T|null {
if (resultArray.length > 0) {
if (pickType === "FIRST") {
return resultArray[0];
} else if (pickType === "LAST") {
return resultArray[resultArray.length - 1];
} else {
const randomIndex = Math.floor(Math.random() * resultArray.length);
return resultArray[randomIndex];
}
} else {
return null;
}
}