Code Monkey home page Code Monkey logo

js-learn's Introduction

js-learn

Stuff I learn about js

MutationObservers, WeakMap, WeakSet JS, CustomElements

Also sharedWorker with websocket. https://blog.pusher.com/reduce-websocket-connections-with-shared-workers/

Build your own approach to designing components without framework.

Updating npm packages

$ npm outdated
$ npm i -g npm-check-updated

$ ncu -u
$ npm i

Object-Oriented

Wrap domain model

  • wrap with empty, zero, value, map, find, where, view, parse, validate, label, with, fulfill, sortable your, filterableby

Remove unused node modules

$ find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;

Semantic versioning

NPM has a command to handle major/minor/patch versioning:

$ npm version

Change NVM version when .nvmrc exists

Create .nvmrc file:

node -v > .nvmrc

Your .nvmrc:

v10.16.2

Add to .zshrc:

autoload -U add-zsh-hook
load-nvmrc() {
  local node_version="$(nvm version)"
  local nvmrc_path="$(nvm_find_nvmrc)"

  if [ -n "$nvmrc_path" ]; then
    local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

    if [ "$nvmrc_node_version" = "N/A" ]; then
      nvm install
    elif [ "$nvmrc_node_version" != "$node_version" ]; then
      nvm use
    fi
  elif [ "$node_version" != "$(nvm version default)" ]; then
    echo "Reverting to nvm default version"
    nvm use default
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

Proxy?

Simplify write to local storage with this:

const auth = {
  get token () { 
    return window.localStorage.getItem('accessToken')
  },
  set token (token) {
    token 
    ? window.localStorage.setItem('accessToken', token)
    : window.localStorage.removeItem('accessToken')
  }
}

Using symbol for non-conflicting key value

// Use it to attach metadata, ghost value etc


const name = Symbol("name")

const obj = {
  name: 'hello world'
}

obj[name] = 'secret name'

console.log(obj, obj.name)
console.log(obj[name])

for (let key in obj) {
  console.log('key', key)
}
console.log(name === 'name')

function greet(n) {
  switch (n) {
    case name:
      console.log('heloo')
  }
}
greet('name')

State machine

class State {
  #value = ''
  #transitions = []
  constructor(transitions, initialValue) {
    this.#transitions = transitions
    this.#value = initialValue
  }

  set value(value) {
	// Reduce it to a nested map, then use checking.
    for (let {
        from,
        to,
        fn
      } of this.#transitions) {
      if (from === this.#value && to === value) {
        fn.call(fn)
      }
    }
    this.#value = value
  }

  get value() {
    return this.#value
  }
}


const s = new State([{
    from: 'a', // Allow nested objects.
    to: 'b',
    fn: () => console.log('go to a')
  },
  {
    from: 'b',
    to: 'a',
    fn: () => console.log('back to be a')
  }
], 'a')
s.value = 'b'
s.value = 'a'

js-learn's People

Contributors

alextanhongpin avatar

Stargazers

uvacoder avatar Vanessa avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.