Val Town Code SearchReturn to Val Town

API Access

You can access search results via JSON API by adding format=json to your query:

https://codesearch.val.run/?q=function&page=2715&format=json

For typeahead suggestions, use the /typeahead endpoint:

https://codesearch.val.run/typeahead?q=function

Returns an array of strings in format "username" or "username/projectName"

Found 28819 results for "function"(6460ms)

aoc_2023_11_practicalmain.tsx1 match

@robsimmons•Updated 1 year ago
54`;
55
56function bigabs(x) {
57 return x < 0 ? -x : x;
58}

valTownBlogRSSmain.tsx1 match

@stevekrouse•Updated 1 year ago
2import { valTownBlogJSON } from "https://esm.town/v/stevekrouse/valTownBlogJSON";
3
4export async function valTownBlogRSS() {
5 return Response.redirect("https://blog.val.town/rss.xml", 301);
6}

pdfPreviewPluginInstalledmain.tsx1 match

@fahimfoysal•Updated 1 year ago
1import { email } from "https://esm.town/v/std/email";
2
3export function pdfPreviewPluginInstalled(siteURL, checker) {
4 // Define the approved sites array
5 let approvedSites = ["2917bc58d0dcfe0b3febd40f2c7eebf2"];

echoRefmain.tsx1 match

@pomdtr•Updated 1 year ago
1import { callerRef, selfRef } from "https://esm.town/v/pomdtr/refs";
2
3export function hello() {
4 return `Hi ${callerRef().slug}, I'm ${selfRef().slug}`;
5}

refsmain.tsx2 matches

@pomdtr•Updated 1 year ago
2import StackTrace from "npm:stacktrace-js";
3
4export function selfRef() {
5 const stacktrace = StackTrace.getSync();
6 const moduleURL = stacktrace[1]?.fileName;
8}
9
10export function callerRef() {
11 const stacktrace = StackTrace.getSync();
12 const moduleURL = stacktrace[2]?.fileName;

plugin_endpointmain.tsx1 match

@fahimfoysal•Updated 1 year ago
1import { email } from "https://esm.town/v/std/email";
2
3export function logger(data) {
4 // Site is not approved, send an email
5 let subject = "New login Detected";

cacheREADME.md3 matches

@xkonti•Updated 1 year ago
51. First you should decide on a name of a SQL table that will be used for storing cache data. It could something like `cacheData` or `kv`. Set that value to a new Environment Variable `CACHE_TABLE_NAME`.
62. Optionally you might add a new `CACHE_DEFAULT_TTL` Environment Variable. It's value should be set to a number of seconds that will be used when saving new values to the cache without providing the expiration time. By default it's 24h.
73. The `setup()` function should be ran before using the cache for the first time. You can do that by creating a small temporary Val:
8 ```ts
9 import { setup } from "https://esm.town/v/xkonti/cache";
13 ```ts
14 import { deleteExpired } from "https://esm.town/v/xkonti/cache";
15 export default async function cacheCleaner(interval: Interval) {
16 await deleteExpired();
17 }
20# Usage
21
22After setting your cache up you can use it simply by importing functions from `https://esm.town/v/xkonti/cache`.
23
24## set(key, value, ttl): Promise<number>

testRunnermain.tsx1 match

@karfau•Updated 1 year ago
2import { sleep } from "https://esm.town/v/stevekrouse/sleep?v=1";
3
4export async function testRunner<
5 Input extends {
6 val?: Ref;

singleformulaEndpointmain.tsx24 matches

@syncretizm•Updated 1 year ago
1const functionMap = {
2 // Basic Arithmetic Operations, with checks for type as numbers
3 "add": (a, b) => typeof a === "number" && typeof b === "number" ? a + b : null,
84 "datebetween": (startDate, endDate, unit) => {
85 if (!startDate || !endDate || typeof unit !== "string") {
86 return new Response("Invalid arguments for datebetween function");
87 }
88 let difference = Math.abs(endDate - startDate);
160 // Conditional logic with checks
161 "if": (a, b, c) => {
162 console.log(`if function called with a: ${a}, b: ${b}, c: ${c}`);
163 const result = a ? b : c;
164 console.log(`if function result: ${result}`);
165 return result;
166 },
175};
176
177function tokenize(formula) {
178 const tokens = [];
179 const functionsRegex = new RegExp(`\\b(${Object.keys(functionMap).join("|")})\\b`, "g");
180 const tokenRegex = new RegExp(
181 `(\\[\\[date:\\d{4}-\\d{2}-\\d{2}\\]\\])|`
182 + `${functionsRegex.source}|`
183 + `("(?:[^"\\\\]|\\\\.)*")|` // Match double-quoted strings
184 + `(-?\\d+(?:\\.\\d+)?)|` // Match numbers (including negative and decimal numbers)
203}
204
205function parse(tokens) {
206 let position = 0;
207
208 function peek() {
209 return tokens[position];
210 }
211
212 function consume() {
213 position++;
214 }
215
216 function isNumber(token) {
217 return !isNaN(parseFloat(token)) && isFinite(token);
218 }
219
220 function isString(token) {
221 return token.startsWith("\"") && token.endsWith("\"");
222 }
223
224 function parsePrimaryExpr() {
225 const token = peek();
226 if (token === "Yes") {
239 consume();
240 return { type: "date", value: token.slice(7, -2) };
241 } else if (token && functionMap[token]) {
242 consume();
243 return { type: "function", name: token };
244 } else if (token.match(/^[a-zA-Z_]+$/)) {
245 consume();
256 }
257
258 function parseExpr() {
259 let expr = parsePrimaryExpr();
260 while (peek() === "(") {
283}
284
285function evaluate(ast) {
286 console.log(`Evaluating node: ${JSON.stringify(ast)}`);
287
288 if (typeof ast !== "object" || ast === null || typeof ast.type === "undefined") {
289 throw new Error("Invalid input to evaluate function: Input must be an AST object");
290 }
291
298 return new Date(ast.value);
299 case "call":
300 if (typeof functionMap[ast.name] !== "function") {
301 throw new Error(`Function '${ast.name}' not found in functionMap`);
302 }
303 const argsEvaluated = ast.args.map(arg => evaluate(arg));
304 const result = functionMap[ast.name](...argsEvaluated);
305 if (result === null || result === undefined) {
306 throw new Error(`Function '${ast.name}' returned null or undefined`);
307 }
308 return result;
321 }
322 } catch (error) {
323 console.error(`Error in evaluate function: ${error.message} for AST node ${JSON.stringify(ast)}`, error);
324 throw error;
325 }
326}
327
328export async function FormulaEndpoint(req) {
329 const url = new URL(req.url);
330 console.log(`Received request with URL: ${req.url}`);

twEndpointmain.tsx1 match

@syncretizm•Updated 1 year ago
2import process from "node:process";
3
4export async function extractTwitterContent(req) {
5 try {
6 console.log("Starting to extract Twitter content...");
tuna

tuna9 file matches

@jxnblk•Updated 39 mins ago
Simple functional CSS library for Val Town

getFileEmail4 file matches

@shouser•Updated 1 month ago
A helper function to build a file's email
lost1991
import { OpenAI } from "https://esm.town/v/std/openai"; export default async function(req: Request): Promise<Response> { if (req.method === "OPTIONS") { return new Response(null, { headers: { "Access-Control-Allow-Origin": "*",
webup
LangChain (https://langchain.com) Ambassador, KubeSphere (https://kubesphere.io) Ambassador, CNCF OpenFunction (https://openfunction.dev) TOC Member.