빠른 참조

101 JavaScript 유틸리티 치트시트

함수형 프로그래밍 스타일로 불변 데이터를 다루기 위한 JavaScript 유틸리티 라이브러리 101 빠른 참조 가이드.

· 게시일

101은 함수형 프로그래밍 스타일로 불변(immutable) 데이터를 조작하기 위한 JavaScript 유틸리티 라이브러리입니다.

사용법 (Usage)

const isObject = require('101/isObject')
isObject({}) // → true

번들 크기를 최소화하기 위해 각 함수가 개별 모듈로 제공됩니다.

See: 101 GitHub 저장소

타입 검사 (Type Checking)

isObject({})
isString('str')
isRegExp(/regexp/)
isBoolean(true)
isEmpty({})
isFunction(x => x)
isInteger(10)
isNumber(10.1)
instanceOf(obj, 'string')

객체 조작 (Objects)

예제

let obj = {}

업데이트 (Update)

obj = put(obj, 'user.name', 'John')
// → { user: { name: 'John' } }

조회 (Read)

pluck(name, 'user.name')
// → 'John'

삭제 (Delete)

obj = del(obj, 'user')
// → { }

접근자 (Accessors)

pluck(state, 'user.profile.name')
pick(state, ['user', 'ui'])
pick(state, /^_/)

pluck은 특정 중첩 값을 검색하고, pick은 객체의 하위 집합을 반환합니다.

See: pluck, pick

설정자 (Setters)

put(state, 'user.profile.name', 'john')

See: put

삭제 (Deletion)

del(state, 'user.profile')
omit(state, ['user', 'data'])

omit은 del과 유사하지만 여러 키를 동시에 제거할 수 있습니다.

See: omit, del

키 경로 검증 (Keypath Verification)

hasKeypaths(state, ['user'])
hasKeypaths(state, { 'user.profile.name': 'john' })

See: hasKeypaths

값 추출 (Values)

values(state)

함수 유틸리티 (Functions)

컴비네이터 및 기본 함수

함수 동등한 표현식
and(x, y) x && y
or(x, y) x || y
xor(x, y) !(!x && !y) && !(x && y)
equals(x, y) x === y
exists(x) !!x
not(x) !x

포인트 프리(point-free) 함수형 파이프라인 및 함수 합성에 적합합니다.

See: and, equals, exists

함수 합성 (Composition)

compose(f, g)       // x => f(g(x))
curry(f)            // x => y => f(x, y)
flip(f)             // f(x, y) --> f(y, x)

See: compose, curry, flip

논리곱 및 논리합 (Conjunction & Disjunction)

passAll(f, g)       // x => f(x) && g(x)
passAny(f, g)       // x => f(x) || g(x)

See: passAll, passAny

수렴 (Converge)

converge(and, [pluck('a'), pluck('b')])(x)
// → and(pluck(x, 'a'), pluck(x, 'b'))

See: converge


배열 조작 (Arrays)

검색 및 술어 (Search & Predicates)

find(list, x => x.y === 2)
findIndex(list, x => ...)
includes(list, 'item')
last(list)
find(list, hasProps('id'))

그룹화 및 인덱싱

groupBy(list, 'id')
indexBy(list, 'id')

실제 활용 예제

술어 함수 합성

isFloat = passAll(isNumber, compose(isInteger, not))
// n => isNumber(n) && not(isInteger(n))
function doStuff (object, options) { ... }

doStuffForce = curry(flip(doStuff))({ force: true })

참고 자료

관련 주제