All Articles

Boolean() in JavaScript

Uber μ—μ„œ λ§Œλ“  react-vis λ₯Ό κ΅¬κ²½ν•˜λ‹€κ°€ 쀑간에 if()μ•ˆμ— λ‹€μ‹œ Boolean() ν•¨μˆ˜λ₯Ό μ¨μ„œ if(Boolean())으둜 값을 κ²€μ‚¬ν•˜λŠ” 뢀뢄이 μžˆμ—ˆλ‹€ 링크

export function applyTransition(props, elements) {
  let { animation } = props
  if (Boolean(animation)) {
    if (!(animation instanceof Object)) {
      animation = DEFAULT_ANIMATION
    }
    return elements.transition().duration(animation.duration)
  }
  return elements
}

ν˜Ήμ‹œ Boolean()이 λ™μž‘μ΄ λ‹€λ₯ΌκΉŒν•΄μ„œ 이것저것 λΉ„κ΅ν•΄λ³΄λŠ” μ½”λ“œλ₯Ό μ§œλ³΄μ•˜μœΌλ‚˜

var values = {
  _true: true,
  _false: false,
  _string: 'hello',
  _stringEmpty: '',
  _number: 1,
  _numberZero: 0,
  _null: null,
  _emptyObject: {},
  _filledObject: { a: 1 },
  _emptyArray: [],
  _filledArray: [1],
}
var valueNames = Object.getOwnPropertyNames(values)

valueNames.forEach(function(valueName, idx) {
  var value = values[valueName]
  if (Boolean(value) == true) console.log(valueName + ' true in Boolean()')
  else console.log(valueName + ' false in Boolean()')

  if (value) console.log(valueName + ' true in if()')
  else console.log(valueName + ' false in if()')
})

λ”±νžˆ evaluation 이 λ‹€λ₯΄μ§€λŠ” μ•Šλ‹€.

심지어 κ·Έ λ°‘μ˜ λ‹€λ₯Έ ν•¨μˆ˜μ—μ„œλŠ” κ·Έλƒ₯ if()λ₯Ό μ“΄λ‹€. λ”±νžˆ μš©λ„κ°€ λ‹€λ₯Έ 것 κ°™μ§€λŠ” μ•Šμ€λ°β€¦

export function getCSSAnimation(props, style) {
  let { animation } = props
  let transition = 'none'
  if (animation) {
    // Fallback to default animation if no animation is passed.
    if (!(animation instanceof Object)) {
      animation = DEFAULT_ANIMATION
    }
    const keys = Object.keys(style)
    // Create a string of visualized parameters and filter out those, which
    // don't have values.
    const animationKeys = keys
      .filter(key => Boolean(style[key]))
      .map(key => `${key} ${animation.duration / 1000}s`)
    if (animationKeys.length) {
      transition = animationKeys.join(',')
    }
  }
  return {
    transition,
    ...style,
  }
}

μ € μ‚¬λžŒλ“€μ΄ μ½”λ“œλ¦¬λ·°λ₯Ό μ•ˆ ν–ˆμ„λ¦¬λ„ μ—†κ³ , μ•„μ•„μ•„μ•„ μ§„μ§œ λͺ¨λ₯΄κ² λ‹€γ…œγ…œγ…œ.

Published 26 May 2016

If I keep marking the dots, someday they will πŸ”—πŸ”—
Hyeungshik Jung on Twitter