diff --git a/docs/index.html b/docs/index.html index ed043c298..713a610aa 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1,134 +1,33 @@ -
30 seconds of codeCurated collection of useful JavaScript snippets
that you can understand in 30 seconds or less.
296
snippets
113
contributors
3096
commits
19557
stars
The core goal of 30 seconds of code is to provide a quality resource for beginner and advanced JavaScript developers alike. We want to help improve the JavaScript ecosystem, by lowering the barrier of entry for newcomers and help seasoned veterans pick up new tricks and remember old ones. In order to achieve this, we have collected hundreds of snippets that can be of use in a wide range of situations. We welcome new contributors and we like fresh ideas, as long as the code is short and easy to grasp in about 30 seconds. The only catch, if you may, is that many of our snippets are not perfectly suited for large, enterprise applications and they might not be deemed production-ready.
In order for 30 seconds of code to be as accessible and useful as possible, all of the snippets in the collection are licensed under the CC0-1.0 License, meaning they are absolutely free to use in any project you like. If you like what we do, you can always credit us, but that is not mandatory.
Our sophisticated robot helpers pick new snippets from our collection daily, so that you can discover new snippets to enhance your projects:
Performs a deep comparison between two values to determine if they are equivalent.
Check if the two values are identical, if they are both Date objects with the same time, using Date.getTime() or if they are both non-object values with an equivalent value (strict comparison). Check if only one value is null or undefined or if their prototypes differ. If none of the above conditions are met, use Object.keys() to check if both values have the same number of keys, then use Array.every() to check if every key in the first value exists in the second one and if they are equivalent by calling this method recursively.
const equals = (a, b) => { - if (a === b) return true; - if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime(); - if (!a || !b || (typeof a != 'object' && typeof b !== 'object')) return a === b; - if (a === null || a === undefined || b === null || b === undefined) return false; - if (a.prototype !== b.prototype) return false; - let keys = Object.keys(a); - if (keys.length !== Object.keys(b).length) return false; - return keys.every(k => equals(a[k], b[k])); +30 seconds of code
30 seconds of code
Curated collection of useful JavaScript snippets
that you can understand in 30 seconds or less.298
snippets114
contributors3117
commits19573
stars- -); // true -isLowerCase('Ab4'); // false - -Our philosophy
The core goal of 30 seconds of code is to provide a quality resource for beginner and advanced JavaScript developers alike. We want to help improve the JavaScript ecosystem, by lowering the barrier of entry for newcomers and help seasoned veterans pick up new tricks and remember old ones. In order to achieve this, we have collected hundreds of snippets that can be of use in a wide range of situations. We welcome new contributors and we like fresh ideas, as long as the code is short and easy to grasp in about 30 seconds. The only catch, if you may, is that many of our snippets are not perfectly suited for large, enterprise applications and they might not be deemed production-ready.
In order for 30 seconds of code to be as accessible and useful as possible, all of the snippets in the collection are licensed under the CC0-1.0 License, meaning they are absolutely free to use in any project you like. If you like what we do, you can always credit us, but that is not mandatory.
-Today's picks
Our sophisticated robot helpers pick new snippets from our collection daily, so that you can discover new snippets to enhance your projects:
-hz
Returns the number of times a function executed per second.
hzis the unit forhertz, the unit of frequency defined as one cycle per second.Use
performance.now()to get the difference in milliseconds before and after the iteration loop to calculate the time elapsed executing the functioniterationstimes. Return the number of cycles per second by converting milliseconds to seconds and dividing it by the time elapsed. Omit the second argument,iterations, to use the default of 100 iterations.const hz = (fn, iterations = 100) => { + const before = performance.now(); + for (let i = 0; i < iterations; i++) fn(); + return 1000 * iterations / (performance.now() - before); }; -equals({ a: [2, { e: 3 }], b: [4], c: 'foo' }, { a: [2, { e: 3 }], b: [4], c: 'foo' }); // true --isLowerCase
-Checks if a string is lower case.
Convert the given string to lower case, using
String.toLowerCase()and compare it to the original.const isLowerCase = str => str === str.toLowerCase(); -isLowerCase('abc'); // true -isLowerCase('a3@ -
-------Getting started
--
- If you are new to JavaScript, we suggest you start by taking a look at the Beginner's snippets
-- If you want to level up tour JavaScript skills, check out the full Snippet collection
-- If you want to see how the project was built and contribute, visit our Github repository
-- If you want to check out some more complex snippets, you can visit the Archive
-
-----Related projects
-The idea behind 30 seconds of code has inspired some people to create similar collections in other programming languages and environments. Here are the ones we like the most:
--
- 30 seconds of CSS by atomiks
-- 30 seconds of Python by kriadmin
-
-----Top contributors
------- Angelos Chalaris -
--- Stefan Feješ -
--- Felix Wu -
--- atomiks -
--- King David Martins -
--- Rohit Tanwar -
--- Soorena Soleimani -
--- Robert Mennell -
----How to contribute
-Do you have a cool idea for a new snippet? Maybe some code you use often and is not part of our collection? Contributing to 30 seconds of code is as simple as 1,2,3,4!
-- 1 ----Create
-Start by creating a snippet, according to the snippet template. Make sure to follow these simple guidelines:
--
-- Your snippet title must be unique and the same as the name of the implemented function.
-- Use the snippet description to explain what your snippet does and how it works.
-- Try to keep the snippet's code short and to the point. Use modern techniques and features.
-- Remember to provide an example of how your snippet works.
-- Your snippet should solve a real-world problem, no matter how simple.
-- Never modify README.md or any of the HTML files.
-- 2 ----Tag
-Run
-npm run taggerfrom your terminal, then open the tag_database file and tag your snippet appropriately. Multitagging is also supported, just make sure the first tag you specify is on of the major tags and the one that is most relevant to the implemneted function.- 3 ----Test
-You can optionally test your snippet to make our job easier. Simply run
-npm run testerto generate the test files for your snippet. Find the related folder for you snippet under the test directory and write some tests. Remember to runnpm run testeragain to make sure your tests are passing.- 4 ----Pull request
-If you have done everything mentioned above, you should now have an awesome snippet to add to our collection. Simply start a pull request and follow the guidelines provided. Remember to only submit one snippet per pull request, so that we can quickly evaluate and merge your code into the collection.
-If you need additional pointers about writing a snippet, be sure to read the complete contribution guidelines.
-
- - --median
-Returns the median of an array of numbers.
-Find the middle of the array, use
-Array.sort()to sort the values. -Return the number at the midpoint iflengthis odd, otherwise the average of the two middle numbers.const median = arr => { - const mid = Math.floor(arr.length / 2), - nums = [...arr].sort((a, b) => a - b); - return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2; -}; -median([5, 6, 50, 1, -5]); // 5 --
- - - - -------Getting started
--
- If you are new to JavaScript, we suggest you start by taking a look at the Beginner's snippets
-- If you want to level up tour JavaScript skills, check out the full Snippet collection
-- If you want to see how the project was built and contribute, visit our Github repository
-- If you want to check out some more complex snippets, you can visit the Archive
-
-----Related projects
-The idea behind 30 seconds of code has inspired some people to create similar collections in other programming languages and environments. Here are the ones we like the most:
--
- 30 seconds of CSS by atomiks
-- 30 seconds of Python by kriadmin
-
-----Top contributors
------- Angelos Chalaris -
--- Stefan Feješ -
--- Felix Wu -
--- atomiks -
--- King David Martins -
--- Rohit Tanwar -
--- Soorena Soleimani -
--- Robert Mennell -
----How to contribute
-Do you have a cool idea for a new snippet? Maybe some code you use often and is not part of our collection? Contributing to 30 seconds of code is as simple as 1,2,3,4!
-- 1 ----Create
-Start by creating a snippet, according to the snippet template. Make sure to follow these simple guidelines:
--
-- Your snippet title must be unique and the same as the name of the implemented function.
-- Use the snippet description to explain what your snippet does and how it works.
-- Try to keep the snippet's code short and to the point. Use modern techniques and features.
-- Remember to provide an example of how your snippet works.
-- Your snippet should solve a real-world problem, no matter how simple.
-- Never modify README.md or any of the HTML files.
-- 2 ----Tag
-Run
-npm run taggerfrom your terminal, then open the tag_database file and tag your snippet appropriately. Multitagging is also supported, just make sure the first tag you specify is on of the major tags and the one that is most relevant to the implemneted function.- 3 ----Test
-You can optionally test your snippet to make our job easier. Simply run
-npm run testerto generate the test files for your snippet. Find the related folder for you snippet under the test directory and write some tests. Remember to runnpm run testeragain to make sure your tests are passing.- 4 ----Pull request
-If you have done everything mentioned above, you should now have an awesome snippet to add to our collection. Simply start a pull request and follow the guidelines provided. Remember to only submit one snippet per pull request, so that we can quickly evaluate and merge your code into the collection.
-If you need additional pointers about writing a snippet, be sure to read the complete contribution guidelines.
-
- - \ No newline at end of file +}; \ No newline at end of file diff --git a/test/hz/hz.js b/test/hz/hz.js new file mode 100644 index 000000000..249ef1743 --- /dev/null +++ b/test/hz/hz.js @@ -0,0 +1,6 @@ +const hz = (fn, iterations = 100) => { +const before = performance.now(); +for (let i = 0; i < iterations; i++) fn(); +return 1000 * iterations / (performance.now() - before); +}; +module.exports = hz; \ No newline at end of file diff --git a/test/hz/hz.test.js b/test/hz/hz.test.js new file mode 100644 index 000000000..afd054442 --- /dev/null +++ b/test/hz/hz.test.js @@ -0,0 +1,13 @@ +const test = require('tape'); +const hz = require('./hz.js'); + +test('Testing hz', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof hz === 'function', 'hz is a Function'); + //t.deepEqual(hz(args..), 'Expected'); + //t.equal(hz(args..), 'Expected'); + //t.false(hz(args..), 'Expected'); + //t.throws(hz(args..), 'Expected'); + t.end(); +}); \ No newline at end of file diff --git a/test/is/is.js b/test/is/is.js index d87edcc74..95b13b7ca 100644 --- a/test/is/is.js +++ b/test/is/is.js @@ -1,2 +1,2 @@ -const is = (type, val) => val instanceof type; +const is = (type, val) => ![, null].includes(val) && val.constructor === type; module.exports = is; \ No newline at end of file diff --git a/test/renameKeys/renameKeys.js b/test/renameKeys/renameKeys.js index 1a02ed11a..5ecf08f90 100644 --- a/test/renameKeys/renameKeys.js +++ b/test/renameKeys/renameKeys.js @@ -1,7 +1,9 @@ -const renameKeys = (keysMap, obj) => Object -.keys(obj) -.reduce((acc, key) => ({ +const renameKeys = (keysMap, obj) => +Object.keys(obj).reduce( +(acc, key) => ({ ...acc, ...{ [keysMap[key] || key]: obj[key] } -}), {}); -module.exports = renameKeys; +}), +{} +); +module.exports = renameKeys; \ No newline at end of file diff --git a/test/testlog b/test/testlog index a4d8eded2..ebf2bfa07 100644 --- a/test/testlog +++ b/test/testlog @@ -1,4 +1,4 @@ -Test log for: Wed Apr 11 2018 20:51:38 GMT+0000 (UTC) +Test log for: Thu Apr 12 2018 20:52:44 GMT+0000 (UTC) > 30-seconds-of-code@0.0.3 test /home/travis/build/Chalarangelo/30-seconds-of-code > tape test/**/*.test.js | tap-spec @@ -691,6 +691,10 @@ Test log for: Wed Apr 11 2018 20:51:38 GMT+0000 (UTC) ✔ httpsRedirect is a Function ✔ Tested on 09/02/2018 by @chalarangelo + Testing hz + + ✔ hz is a Function + Testing inRange ✔ inRange is a Function @@ -762,11 +766,35 @@ Test log for: Wed Apr 11 2018 20:51:38 GMT+0000 (UTC) ✔ Works for sets ✔ Works for weak maps ✔ Works for weak sets - ✔ Works for strings - returns false for primitive + + ✖ Works for strings - returns false for primitive + -------------------------------------------------- + operator: notOk + expected: false + actual: true + at: Test.test (/home/travis/build/Chalarangelo/30-seconds-of-code/test/is/is.test.js:17:10) + stack: |- + ✔ Works for strings - returns true when using constructor - ✔ Works for numbers - returns false for primitive + + ✖ Works for numbers - returns false for primitive + -------------------------------------------------- + operator: notOk + expected: false + actual: true + at: Test.test (/home/travis/build/Chalarangelo/30-seconds-of-code/test/is/is.test.js:19:10) + stack: |- + ✔ Works for numbers - returns true when using constructor - ✔ Works for booleans - returns false for primitive + + ✖ Works for booleans - returns false for primitive + --------------------------------------------------- + operator: notOk + expected: false + actual: true + at: Test.test (/home/travis/build/Chalarangelo/30-seconds-of-code/test/is/is.test.js:21:10) + stack: |- + ✔ Works for booleans - returns true when using constructor ✔ Works for functions @@ -1448,6 +1476,11 @@ Test log for: Wed Apr 11 2018 20:51:38 GMT+0000 (UTC) ✔ removeVowels is a Function + Testing renameKeys + + ✔ renameKeys is a Function + ✔ should be equivalent + Testing reverseString ✔ reverseString is a Function @@ -1970,15 +2003,27 @@ Test log for: Wed Apr 11 2018 20:51:38 GMT+0000 (UTC) ✔ zipWith is a Function ✔ Sends a GET request - ✔ Sends a POST request ✔ Runs the function provided + ✔ Sends a POST request ✔ Runs promises in series ✔ Works as expecting, passing arguments properly ✔ Works with multiple promises - total: 1006 + + Failed Tests: There were 3 failures + + Testing is + + ✖ Works for strings - returns false for primitive + ✖ Works for numbers - returns false for primitive + ✖ Works for booleans - returns false for primitive + + + total: 1009 passing: 1006 - duration: 2.4s + failing: 3 + duration: 2.5s +undefined \ No newline at end of file