Skip to content

Commit ca8d76d

Browse files
committed
try this
1 parent 710ec04 commit ca8d76d

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

src/lib/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ export function escapeHtml(unsafe: string)
149149
.replace(/'/g, "'");
150150
}
151151

152+
export function isIterable(obj: never) {
153+
// checks for null and undefined
154+
if (obj == null) {
155+
return false;
156+
}
157+
return typeof obj[Symbol.iterator] === 'function';
158+
}
159+
152160

153161

154162
export type HistoricalEntry = {

src/routes/tools/test/productPriority/+page.svelte

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
22
33
import {flip} from "svelte/animate";
4+
import { isIterable } from "$lib/utils";
45
56
export let data;
67
@@ -11,13 +12,14 @@
1112
1213
$: {
1314
i;
14-
for (const product of data.products) {
15-
const lastStock = JSON.parse(product.stock);
16-
const velocityInfluence = (product.available && product.purchasesPerHour == -1 ? 100 : product.purchasesPerHour) * (Math.random()/2);
17-
let distance = (Date.now() - product.stockChecked);
18-
if(!product.available && distance > 10) distance *= (Math.random()/32)
19-
rankings[product.handle] = ((distance / (3 * 60 * 60e3)) + velocityInfluence) / (lastStock?.total === 0 ? 100 : (lastStock?.total === -1 ? 10 : 1));
20-
15+
if(isIterable(data.products as never)) {
16+
for (const product of data.products) {
17+
const lastStock = JSON.parse(product.stock);
18+
const velocityInfluence = (product.available && product.purchasesPerHour == -1 ? 100 : product.purchasesPerHour) * (Math.random()/2);
19+
let distance = (Date.now() - product.stockChecked);
20+
if(!product.available && distance > 10) distance *= (Math.random()/32)
21+
rankings[product.handle] = ((distance / (3 * 60 * 60e3)) + velocityInfluence) / (lastStock?.total === 0 ? 100 : (lastStock?.total === -1 ? 10 : 1));
22+
}
2123
}
2224
2325
sortedRankings = Object.entries(rankings).sort((a, b) => b[1] - a[1]);

0 commit comments

Comments
 (0)