Skip to content

Instantly share code, notes, and snippets.

@maxfi
Last active May 6, 2024 11:23
Show Gist options
  • Save maxfi/f71d308f03b638c2b803d93c70cba520 to your computer and use it in GitHub Desktop.
Save maxfi/f71d308f03b638c2b803d93c70cba520 to your computer and use it in GitHub Desktop.
Javascript / Ecmascript notes and references

https://project-awesome.org

General

Learn

DevTools

ES5/ES6/ES7+

Babel/rollup

Package.json / boilerplate

{
  "main": "dist/index.js",
  "jsnext:main": "lib/index.js",
  "scripts": {
    "release": "np",
    "build": "rm -rf dist/* && babel lib --ignore *.test.js --out-dir dist --copy-files",
    "test": "xo && nyc ava",
    "coverage": "nyc report --reporter=text-lcov | coveralls"
  }
}

JSON

Promises

Metaprogramming

Async

Packages

Testing

Linting

Documentation

Errors

Offline

Recipes

lodash/fp

Good examples at sales-app-sales: /imports/api/notifications/group-notifications-by-comment-thread.js

Combine / merge arrays by key

References

Custom

const arr1 = [{ userId: '1' }, { userId: '2'}]
const arr2 = [{_id: '2'}, {_id: '1'}]

// =================


// LODASH
_.zipWith(_.merge,_.sortBy(['userId'], arr1), _.sortBy(['_id'], arr2))
// => [ { "userId": "1", "_id": "1" }, { "userId": "2", "_id": "2" } ]

// ES6+
const combine = (prop1, prop2, one, two) => 
  one.map(x => ({
  ...x,
  ...two.find(y => x[prop1] === y[prop2])
}));

combine('userId', '_id', arr1, arr2)
// => [ { "userId": "1", "_id": "2" }, { "userId": "2", "_id": "2" } ]
combine('userId', '_id', arr1, [{_id: '2'}])
// => [ { "userId": "1" }, { "userId": "2", "_id": "2" } ]

const combineOr = (defaultValue, [prop1, prop2], [one, two]) => 
  one.map(x => ({
  ...x,
  ...(two.find(y => x[prop1] === y[prop2]) || defaultValue)
}));

combineOr({x: 'y'}, ['userId', '_id'], [arr1, [{_id: '2'}]])
// => [ { "userId": "1", "x": "y" }, { "userId": "2", "_id": "2" } ]
const findOr = (def, prop, match) =>
  _.pipe(
    _.find(match),
    _.propOr(def, prop)
  )

const lookup = (defaultValue, prop, match, searchArr, arr) =>
  _.map(x => findOr(defaultValue, prop, match(x))(searchArr), arr)

Pluck/map multiple properties

arr.map(x => _.pick(x, 'propOne', 'propTwo'))

Opposite of _.uniq()

_([1, 1, 2, 2, 3]).groupBy().pickBy(x => x.length > 1).keys().value()
_.transform(_.countBy(array), function(result, count, value) {
  if (count > 1) result.push(value);
}, []);

URLs

Object validation

Cron / scheduled tasks

Math

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment