Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielberndt/eb59230c4ac5c2fd7edaa27dfb2b2e89 to your computer and use it in GitHub Desktop.
Save danielberndt/eb59230c4ac5c2fd7edaa27dfb2b2e89 to your computer and use it in GitHub Desktop.
Codecks Gists
let subdomain = SUBDOMAIN;
let segmentQuery = {
$and: [
{ startedAt: { op: "gte", value: "2020-11-03T23:00:00.000Z" } },
{ startedAt: { op: "lt", value: "2020-12-01T23:00:00.000Z" } },
],
user: { name: ["daniel","tom","Alex"] },
};
let query = {
_root: [
{
account: [
{
[`timeTrackingSegments(${JSON.stringify(segmentQuery)})`]: [
"startedAt",
"finishedAt",
"modifyDurationMsBy",
"id",
{
user: ["name"],
card: [
"accountSeq",
"title",
{
deck: ["title"],
parentCard: ["title"],
},
],
},
],
},
],
},
],
};
await fetch(`https://api.codecks.io?query=${JSON.stringify(query)}&x-account=${subdomain}`).then(r => r.json())
let subdomain = YOUR_SUBDOMAIN
let deckNumber = TAKE_NUMBER_FROM_URL
let cardQuery = `cards({"deck":{"accountSeq":${deckNumber}},"coverFileId":null})`
let query = {_root: [{account: [{[cardQuery]: [{attachments:[{file:["id","meta"]}]}]}]}]}
let result = await fetch(`https://api.codecks.io?query=${JSON.stringify(query)}&x-account=${subdomain}`).then(r => r.json())
for (let cardId of result.account[result._root.account][cardQuery]) {
let card = result.card[cardId]
if (!card.attachments.length) continue;
let files = card.attachments.map(a => result.file[result.attachment[a].file])
let img = files.find(f => f.meta.type)
if (!img) continue
await fetch(`https://api.codecks.io/dispatch/cards/update?x-account=${subdomain}`, {
headers: {"content-type": "application/json"},
body: JSON.stringify({id: cardId, coverFileId: img.id}),
method: "POST",
});
await new Promise(r => setTimeout(r, 100))
}
const lettersToSequence = (
letters: string,
{startVal = 0, implicitZero = false}: {startVal: number; implicitZero: boolean} = {
startVal: 0,
implicitZero: false,
}
) => {
const length = letters.length;
const letterToIndex = letters.split("").reduce((memo, letter, index) => {
memo[letter] = index;
return memo;
}, {} as {[l: string]: number});
return {
intToSeq(intVal: number) {
const seq = [];
let q = intVal + startVal;
if (implicitZero) q += 1;
let r;
do {
if (implicitZero) q += -1;
r = q % length;
q = Math.floor(q / length);
seq.unshift(letters[r]);
} while (q);
return seq.join("");
},
seqToInt(seq: string) {
let intVal = letterToIndex[seq[0]] || 0;
for (let i = 1; i < seq.length; i += 1) {
if (implicitZero) intVal += 1;
intVal *= length;
intVal += letterToIndex[seq[i]];
}
return intVal - startVal;
},
letters,
};
};
export const pronounceSafeSeq = lettersToSequence("123456789acefghijkoqrsuvwxyz", {
startVal: 28 * 29 - 1,
implicitZero: true,
});
// use like this: pronounceSafeSeq.seqToInt('13c') -> 67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment