Skip to content

Instantly share code, notes, and snippets.

View okovalov's full-sized avatar
🏠
Working from home

Oleksandr Kovalov okovalov

🏠
Working from home
View GitHub Profile
@okovalov
okovalov / .eslintrc.js
Created March 3, 2019 21:33 — forked from nkbt/.eslintrc.js
Strict ESLint config for React, ES6 (based on Airbnb Code style)
{
"env": {
"browser": true,
"node": true,
"es6": true
},
"plugins": ["react"],
"ecmaFeatures": {
--
-- There is a `t` table with 3 fields:
-- `uid` - user ID
-- `dt` - date and time of message
-- `s` - text of the message
-- Write an SQL query to retrieve date and text of the last message for all users.
-- schema
CREATE TABLE `user_message_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
/**
Find the minimum value in an array of strings using a loop. For example, for this array:
a=['my','name','is','john','doe'];
**/
const sortByLengthOnly = arr => arr.sort( (a, b) => a.length - b.length)
const sortByLengthAndValue = arr => arr.sort( (a, b) => {
const factorial = n => n < 0 ? 'n must be > 0' : n < 2 ? 1 : n * factorial(n - 1)
for (let i = -2; i <= 10 ; i++ ) {
console.time(`Time for ${i}`);
console.log(`factorial(${i}) = ${factorial(i)}`)
console.timeEnd(`Time for ${i}`)
}
console.log('-----')

JavaScript Private Class Fields

taken from https://flaviocopes.com/javascript-private-class-fields/

Introduction and code samples on using private class fields in JavaScript.

Before the introduction of private class fields, we could not really enforce private properties on a class. We used conventions instead, maybe using _ as an hint that the field is private, like this:

class Counter {
@okovalov
okovalov / delete_git_submodule.md
Created October 22, 2019 17:30 — forked from myusuf3/delete_git_submodule.md
How effectively delete a git submodule.

To remove a submodule you need to:

  • Delete the relevant section from the .gitmodules file.
  • Stage the .gitmodules changes git add .gitmodules
  • Delete the relevant section from .git/config.
  • Run git rm --cached path_to_submodule (no trailing slash).
  • Run rm -rf .git/modules/path_to_submodule (no trailing slash).
  • Commit git commit -m "Removed submodule "
  • Delete the now untracked submodule files rm -rf path_to_submodule
/**
* Checks if dates are on the same day
*
* @param {Object} first First date to comapre
* @param {Object} second Second date to compare
*/
const datesAreOnSameDay = (first, second) =>
first.getFullYear() === second.getFullYear() &&
first.getMonth() === second.getMonth() &&
first.getDate() === second.getDate()
// An example of recursive flattering of a given data (potentially multidimensional)
// 1.
// concat all the arrays of a given array and return a new array with a result
const concat = arr => [].concat(...arr)
// recursively flattern a given data into a new array (ES6)
const flattern = el => Array.isArray(el) ? concat(el.map(flattern)) : Array.of(el)
How to make a page editable in the browser
There is a special and pretty secret mode in browsers, called design mode.
When you set a page into design mode, you can edit the content of the page directly inside the browser page, which is very handy to test some prototype or check out how a new headline would look, for example.
How do you enable it? Open the DevTools console, and type:
document.designMode = 'on'
Read:
Most of them are based on streams, like csv-parser or node-csv.
Those are great to deal with CSV in a production system.
I like to keep things simple when I don’t have performance in mind. For example, for a one-time parsing of CSV that I had to do to consolidate my backend systems.
To do so, I used neat-csv, a package that exposes the csv-parser functionality to a simple async/await interface.
Install it using npm install neat-csv and require it in your app: