diff --git a/docs/archive.html b/docs/archive.html index e841ac185..d4e97f0ec 100644 --- a/docs/archive.html +++ b/docs/archive.html @@ -10,7 +10,7 @@ gtag('config', 'UA-117141635-1'); - Snippets for Beginners - 30 seconds of code + Snippets Archive - 30 seconds of code diff --git a/docs/beginner.html b/docs/beginner.html index 101a5db4c..162c1f056 100644 --- a/docs/beginner.html +++ b/docs/beginner.html @@ -93,7 +93,7 @@
-

Beginner snippets

+

Snippets for Beginners

The following section is aimed towards individuals who are at the start of their web developer journey. Each snippet in the next section is simple yet very educational for newcomers. This section is by no means a complete resource for learning modern JavaScript. However, it is enough to grasp some common concepts and use cases. We also strongly recommend checking out MDN web docs as a learning resource.


diff --git a/docs/index.html b/docs/index.html index 3fd8cfb4e..db7503e7e 100644 --- a/docs/index.html +++ b/docs/index.html @@ -76,42 +76,32 @@
-

createElement

-

Creates an element from a string (without appending it to the document). -If the given string contains multiple elements, only the first one will be returned.

-

Use document.createElement() to create a new element. -Set its innerHTML to the string supplied as the argument. -Use ParentNode.firstElementChild to return the element version of the string.

-
const createElement = str => {
-  const el = document.createElement('div');
-  el.innerHTML = str;
-  return el.firstElementChild;
+      

difference

+

Returns the difference between two arrays.

+

Create a Set from b, then use Array.filter() on a to only keep values not contained in b.

+
const difference = (a, b) => {
+  const s = new Set(b);
+  return a.filter(x => !s.has(x));
 };
-
const el = createElement(
-  `<div class="container">
-    <p>Hello!</p>
-  </div>`
-);
-console.log(el.className); // 'container'
+
difference([1, 2, 3], [1, 2, 4]); // [3]
 
-

isSymbol

-

Checks if the given argument is a symbol.

-

Use typeof to check if a value is classified as a symbol primitive.

-
const isSymbol = val => typeof val === 'symbol';
-
isSymbol(Symbol('x')); // true
+

hasFlags

+

Check if the current process's arguments contain the specified flags.

+

Use Array.every() and Array.includes() to check if process.argv contains all the specified flags. +Use a regular expression to test if the specified flags are prefixed with - or -- and prefix them accordingly.

+
const hasFlags = (...flags) =>
+  flags.every(flag => process.argv.includes(/^-{1,2}/.test(flag) ? flag : '--' + flag));
+
// node myScript.js -s --test --cool=true
+hasFlags('-s'); // true
+hasFlags('--test', 'cool=true', '-s'); // true
+hasFlags('special'); // false
 
-

scrollToTop

-

Smooth-scrolls to the top of the page.

-

Get distance from top using document.documentElement.scrollTop or document.body.scrollTop. -Scroll by a fraction of the distance from the top. Use window.requestAnimationFrame() to animate the scrolling.

-
const scrollToTop = () => {
-  const c = document.documentElement.scrollTop || document.body.scrollTop;
-  if (c > 0) {
-    window.requestAnimationFrame(scrollToTop);
-    window.scrollTo(0, c - c / 8);
-  }
-};
-
scrollToTop();
+

isPlainObject

+

Checks if the provided value is an object created by the Object constructor.

+

Check if the provided value is truthy, use typeof to check if it is an object and Object.constructor to make sure the constructor is equal to Object.

+
const isPlainObject = val => !!val && typeof val === 'object' && val.constructor === Object;
+
isPlainObject({ a: 1 }); // true
+isPlainObject(new Map()); // false
 

diff --git a/scripts/web.js b/scripts/web.js index 93f3c0406..472b3e361 100644 --- a/scripts/web.js +++ b/scripts/web.js @@ -132,7 +132,15 @@ if(!util.isTravisCI() || (util.isTravisCI() && (process.env['TRAVIS_EVENT_TYPE'] indexStaticFile = indexStaticFile.replace('$daily-picks',indexDailyPicks); // Use the Github API to get the needed data const githubApi = 'api.github.com'; - const headers = { 'User-Agent' : '30-seconds-of-code'}; + const headers = util.isTravisCI() + ? { 'User-Agent' : '30-seconds-of-code', 'Authorization': 'token '+process.env['GH_TOKEN']} + : { 'User-Agent' : '30-seconds-of-code'}; + // Test the API's rate limit (keep for various reasons) + https.get({host: githubApi, path: '/rate_limit?', headers: headers}, res =>{ + res.on('data', function (chunk) { + console.log('Remaining requests: '+JSON.parse(chunk).resources.core.remaining); + }); + }); // Send requests and wait for responses, write to the page https.get({host: githubApi, path: '/repos/chalarangelo/30-seconds-of-code/commits?per_page=1', headers: headers}, resCommits =>{ https.get({host: githubApi, path: '/repos/chalarangelo/30-seconds-of-code/contributors?per_page=1', headers: headers}, resContributors => {