diff --git a/public/consolidated/c.json b/public/consolidated/c.json index 1588cd69..a9258611 100644 --- a/public/consolidated/c.json +++ b/public/consolidated/c.json @@ -38,7 +38,7 @@ "numbers" ], "contributors": [], - "code": "#include\nvoid swap(int* num1,int* num2){\n *num1 = *num1 + *num2;\n *num2 = *num1 - *num2;\n *num1 = *num1 - *num2;\n}\n\n// Usage:\nint a = 3,b = 4;\nswap(&a,&b); // simply use printf after this to print swapped values\n" + "code": "#include\nvoid swap(int* num1,int* num2){\n *num1 = *num1 + *num2;\n *num2 = *num1 - *num2;\n *num1 = *num1 - *num2;\n}\n\n// Usage:\nint a = 3,b = 4;\nswap(&a,&b); // swaps the values of the a and b variables\n" } ] } diff --git a/public/consolidated/javascript.json b/public/consolidated/javascript.json index 2e8c8f97..d56e80ed 100644 --- a/public/consolidated/javascript.json +++ b/public/consolidated/javascript.json @@ -61,6 +61,22 @@ } ] }, + { + "categoryName": "Audio Manipulation", + "snippets": [ + { + "title": "Web Audio API Starter", + "description": "Simple example for playing a tone with the Web Audio API", + "author": "1982FenceHopper", + "tags": [ + "audio", + "web" + ], + "contributors": [], + "code": "/*\n * \"It's recommended to create one AudioContext and reuse it instead of initializing a new one each time,\n * and it's OK to use a single AudioContext for several different audio sources and pipeline concurrently.\"\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/AudioContext\n */\nclass AudioSingleton {\n static ctx;\n\n static getInstance() {\n if (!ctx) {\n ctx = new AudioContext();\n }\n return ctx;\n }\n}\n\n// Usage:\nfunction startAudio() {\n let ctx = AudioSingleton.getInstance();\n\n oscNode = ctx.createOscillator();\n gainNode = ctx.createGain();\n\n gainNode.gain.value = 0.01; // It can get really loud, so this helps :)\n\n oscNode.type = \"square\";\n oscNode.frequency.setValueAtTime(440, ctx.currentTime);\n\n oscNode.connect(gainNode);\n gainNode.connect(ctx.destination);\n\n oscNode.start();\n}\n\nstartAudio(); // Output: A tone should be audible.\n" + } + ] + }, { "categoryName": "Basics", "snippets": [ @@ -93,6 +109,44 @@ } ] }, + { + "categoryName": "Cookies", + "snippets": [ + { + "title": "Delete Cookie", + "description": "Delete a browser cookie", + "author": "1982FenceHopper", + "tags": [ + "cookie", + "document" + ], + "contributors": [], + "code": "const deleteCookie = (name) => {\n try {\n document.cookie = `${encodeURIComponent(name)}=MARKED;max-age=0;path=/`;\n } catch (e) {\n console.error(e);\n }\n};\n\n// Usage:\nlet name = \"foo\";\n\ndeleteCookie(name);\nconsole.log(document.cookie) // Expected: Cookie foo should not be a part of the output.\n" + }, + { + "title": "Get Cookie", + "description": "Get a browser cookie", + "author": "1982FenceHopper", + "tags": [ + "cookie", + "document" + ], + "contributors": [], + "code": "const getCookie = (name) => {\n return document.cookie.split(\";\").find((cookie) => cookie.trim().split(\"=\")[0] == name)\n};\n\n// Usage:\nlet name = \"foo\";\n\nconsole.log(getCookie(name)); // Output: foo=bar\n" + }, + { + "title": "Set Cookie", + "description": "Set a browser cookie", + "author": "1982FenceHopper", + "tags": [ + "cookie", + "document" + ], + "contributors": [], + "code": "const setCookie = (name, value, max_age = 3600, expires = undefined) => {\n\ttry {\n \tdocument.cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)};max-age=${max_age};path=/;${expires ? `expires=${encodeURIComponent(expires)}` : \"\"}`;\n } catch (e) {\n console.error(e);\n }\n};\n\n// Usage:\nlet name = \"foo\";\nlet value = \"bar\";\n\nsetCookie(name, value);\nconsole.log(document.cookie) // Expected: foo=bar should be a part of the output.\n" + } + ] + }, { "categoryName": "Date And Time", "snippets": [ diff --git a/snippets/javascript/cookies/delete-cookie.md b/snippets/javascript/cookies/delete-cookie.md new file mode 100644 index 00000000..125db025 --- /dev/null +++ b/snippets/javascript/cookies/delete-cookie.md @@ -0,0 +1,21 @@ +--- +title: Delete Cookie +description: Delete a browser cookie +author: 1982FenceHopper +tags: cookie, document +--- + +```js +const deleteCookie = (name) => { + try { + document.cookie = `${encodeURIComponent(name)}=MARKED;max-age=0;path=/`; + } catch (e) { + console.error(e); + } +}; + +// Usage: +let name = "foo"; + +deleteCookie(name); +``` \ No newline at end of file diff --git a/snippets/javascript/cookies/get-cookie.md b/snippets/javascript/cookies/get-cookie.md new file mode 100644 index 00000000..31601708 --- /dev/null +++ b/snippets/javascript/cookies/get-cookie.md @@ -0,0 +1,17 @@ +--- +title: Get Cookie +description: Get a browser cookie +author: 1982FenceHopper +tags: cookie, document +--- + +```js +const getCookie = (name) => { + return document.cookie.split(";").find((cookie) => cookie.trim().split("=")[0] == name) +}; + +// Usage: +let name = "foo"; + +console.log(getCookie(name)); // Output: foo=bar +``` \ No newline at end of file diff --git a/snippets/javascript/cookies/set-cookie.md b/snippets/javascript/cookies/set-cookie.md new file mode 100644 index 00000000..d9bc7a1d --- /dev/null +++ b/snippets/javascript/cookies/set-cookie.md @@ -0,0 +1,22 @@ +--- +title: Set Cookie +description: Set a browser cookie +author: 1982FenceHopper +tags: cookie, document +--- + +```js +const setCookie = (name, value, max_age = 3600, expires = undefined) => { + try { + document.cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)};max-age=${max_age};path=/;${expires ? `expires=${encodeURIComponent(expires)}` : ""}`; + } catch (e) { + console.error(e); + } +}; + +// Usage: +let name = "foo"; +let value = "bar"; + +setCookie(name, value); +``` \ No newline at end of file