Skip to content

Instantly share code, notes, and snippets.

View Restuta's full-sized avatar
🦄
Hacking fast and slow.

Anton Vynogradenko Restuta

🦄
Hacking fast and slow.
View GitHub Profile

Cheat sheet: JavaScript Array methods

Deriving a new Array from an existing Array:

['■','●','▲'].slice(1, 3)           ['●','▲']
['■','●','■'].filter(x => x==='■')  ['■','■']
    ['▲','●'].map(x => x+x)         ['▲▲','●●']
    ['▲','●'].flatMap(x => [x,x])   ['▲','▲','●','●']
@sindresorhus
sindresorhus / esm-package.md
Last active May 8, 2024 22:50
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@asktree
asktree / InformationDestruction.ts
Last active February 5, 2021 17:55
TypeScript's type assignment feature destroys information (inconsistently)
// Typescript lets you put constraints on assignment -- cool
// - I don't know what this feature is officially called and can't find it.
// for example, telling the IDE that a constant should be "even"
type even = 2 | 4 | 6;
const a = 2;
const b: even = 2;
const c: even = 4;
const d: even = 3; // error: constraint violation
// you can assert types, this is different!
@marissamarym
marissamarym / Slack Block Kit Cheatsheet
Last active January 16, 2021 22:56
Cheatsheet for the Slack Block Kit Framework
# Slack Block Kit Cheatsheet
This is a cheatsheat for the [Block Kit API](https://api.slack.com/block-kit), great for referencing when wireframing out Slack interactions.
## Values to know
### Surfaces
- [50 blocks per message](https://api.slack.com/reference/block-kit/blocks#:~:text=You%20can%20include%20up%20to,in%20modals%20or%20home%20tabs.)
- [100 blocks per modal or in the home tab](https://api.slack.com/reference/block-kit/blocks#:~:text=You%20can%20include%20up%20to,in%20modals%20or%20home%20tabs.)
@Restuta
Restuta / ora-progress-bar.js
Last active November 8, 2019 17:08
Ora based progress bar
/* eslint-disable no-return-assign */
const chalk = require('chalk');
const ora = require('ora');
const prettyMs = require('pretty-ms');
const throttle = require('lodash/throttle');
const getHeapUsed = throttle(
() => {
const heapUsed = process.memoryUsage().heapUsed / 1024 / 1024;
return Math.round(heapUsed, 2);
@Restuta
Restuta / audit-log-design-problem.js
Last active January 25, 2019 22:37
Audit Log Collection Design Problem
/*
Problem
We have a function that updates objects and generates audit records of the changes.
Think about financial systems or just any app where it's useful to know who made changes to what.
Audit records needs to be saved to the DB as well as updates to objects, but ideally
we would want to execute business logic, accumulate all updates and audit records in memory
and then save it to DB all at once. Let's look at the specifics.
@Restuta
Restuta / mapValuesDeep.js
Last active August 30, 2017 01:06
mapValuesDeep with circular references detection
// based on vanilla lodash, todo: rewrite with lodash/fp
// uses isPlainObject to detect ojbects to go deep into
// detects circular references using Set()
function mapValuesDeep(originalObj, mapFunc) {
const visitedObjects = new Set()
const mapValues = (originalObj, mapFunc) =>
_.mapValues(originalObj, value => {
if (_.isPlainObject(value)) {
@Restuta
Restuta / progress.js
Last active May 5, 2017 22:21
Simple Progress Reporter for JavaScript
// gets time now in milliseconds
const msNow = () => (+new Date())
function createProgress ({
maxItems = 100,
onProgressChange = () => {},
onComplete = () => {}
}) {
let currentProgress = 0 // %
let lastProgress = 0
@ColCh
ColCh / README.md
Last active January 25, 2023 13:31
Git pre-push hook to confirm pushing to master
@acdlite
acdlite / app.js
Last active January 20, 2023 08:23
Quick and dirty code splitting with React Router v4
// getComponent is a function that returns a promise for a component
// It will not be called until the first mount
function asyncComponent(getComponent) {
return class AsyncComponent extends React.Component {
static Component = null;
state = { Component: AsyncComponent.Component };
componentWillMount() {
if (!this.state.Component) {
getComponent().then(Component => {