Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions assignment/src/bind.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { bind } from "./bind"

describe("bind 함수 테스트", () => {
test("bind Basic", () => {
const users = { name: "d5ng" }

function getName(this: { name: string }) {
return `내 이름은 ${this.name}이에요!`
}

const bindingUser = bind(getName, users)
expect(bindingUser()).toBe("내 이름은 d5ng이에요!")
})

test("bind Partial", () => {
function greet(this: { name: string }, greeting: string, punctuation: string) {
return `${greeting}, ${this.name}${punctuation}`
}

const person = { name: "Alice" }
const greetAlice = bind(greet, person, "Hello")

expect(greetAlice("!")).toBe("Hello, Alice!")
})

test("생성자 함수 테스트", () => {
function Person(this: Record<string, any>, name: string) {
this.name = name
}

const BoundPerson = bind(Person, { name: "notUsed" })
const p = new BoundPerson("Tom")

expect(p.name).toBe("Tom")
expect(p instanceof Person).true
})
})
21 changes: 21 additions & 0 deletions assignment/src/bind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type BindThis<T, U> = T extends (this: any, ...args: infer A) => infer R ? (this: U, ...args: A) => R : never

type PartiallyBound<T, P extends any[]> = T extends (...args: [...P, ...infer Rest]) => infer R
? (...args: Rest) => R
: never

export function bind<T extends (this: any, ...args: any[]) => any, U, P extends any[]>(fn: T, thisArg: U, ...args: P) {
function boundFn(this: any, ...args2: any[]) {
const isInstnace = this instanceof boundFn

if (isInstnace) {
return fn.call(this, ...args, ...args2)
}

return fn.call(thisArg, ...args, ...args2) as PartiallyBound<BindThis<T, U>, P>
}

boundFn.prototype = Object.create(fn.prototype)

return boundFn
}