Skip to content
Merged
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
12 changes: 12 additions & 0 deletions challenges/506-object-assign/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*The `Object.assign()` method copies all enumerable own properties from one or more source objects to a target object. It returns the target object.* (source: [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign))

It is widely used, Object Spread operator actually is internally the same as `Object.assign()` ([source](https://github.com/tc39/proposal-object-rest-spread/blob/master/Spread.md)). Following 2 lines of code are totally the same.

```js
let aClone = { ...a };
let aClone = Object.assign({}, a);
```

This is an easy one, could you implement `Object.assign()` with your own implementation ?

**Note**: **Don't use Object.assign() in your code** It doesn't help improve your skills
11 changes: 11 additions & 0 deletions challenges/506-object-assign/info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
difficulty: medium
title: Object.assign()
type: question
template: javascript
tags: javascript
author:
github: jsartisan
name: Pawan Kumar
avatar_url: https://avatars.githubusercontent.com/u/6636360?v=4
published_date: '2025-09-27'

83 changes: 83 additions & 0 deletions challenges/506-object-assign/template.javascript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
```js index.js
export function objectAssign(target, ...sources) {
// TODO: Implement me
}
```

```js index.test.js
import { objectAssign } from './index';

describe('objectAssign', () => {
it('should copy properties from source to target', () => {
const target = { a: 1 };
const source = { b: 2, c: 3 };
const result = objectAssign(target, source);

expect(result).toBe(target);
expect(result).toEqual({ a: 1, b: 2, c: 3 });
});

it('should handle multiple sources', () => {
const target = { a: 1 };
const source1 = { b: 2 };
const source2 = { c: 3 };
const result = objectAssign(target, source1, source2);

expect(result).toEqual({ a: 1, b: 2, c: 3 });
});

it('should overwrite existing properties', () => {
const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };
const result = objectAssign(target, source);

expect(result).toEqual({ a: 1, b: 3, c: 4 });
});

it('should handle null and undefined sources', () => {
const target = { a: 1 };
const result = objectAssign(target, null, undefined, { b: 2 });

expect(result).toEqual({ a: 1, b: 2 });
});

it('should handle primitive sources', () => {
const target = { a: 1 };
const result = objectAssign(target, 'string', 42, true, null);

expect(result).toEqual({ a: 1 });
});

it('should handle empty target and sources', () => {
const result = objectAssign({}, { a: 1 }, {});

expect(result).toEqual({ a: 1 });
});

it('should not copy non-enumerable properties', () => {
const source = {};
Object.defineProperty(source, 'nonEnumerable', {
value: 'test',
enumerable: false
});

const target = {};
const result = objectAssign(target, source);

expect(result).toEqual({});
expect('nonEnumerable' in result).toBe(false);
});

it('should handle symbol properties', () => {
const symbol = Symbol('test');
const target = {};
const source = { [symbol]: 'value', regular: 'prop' };
const result = objectAssign(target, source);

expect(result).toEqual({ regular: 'prop' });
expect(result[symbol]).toBe('value');
});
});
```


83 changes: 83 additions & 0 deletions challenges/506-object-assign/template.typescript.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
```ts index.ts
export function objectAssign<T extends Record<string, any>>(target: T, ...sources: any[]): T {
// TODO: Implement me
}
```

```ts index.test.ts
import { objectAssign } from './index';

describe('objectAssign', () => {
it('should copy properties from source to target', () => {
const target = { a: 1 };
const source = { b: 2, c: 3 };
const result = objectAssign(target, source);

expect(result).toBe(target);
expect(result).toEqual({ a: 1, b: 2, c: 3 });
});

it('should handle multiple sources', () => {
const target = { a: 1 };
const source1 = { b: 2 };
const source2 = { c: 3 };
const result = objectAssign(target, source1, source2);

expect(result).toEqual({ a: 1, b: 2, c: 3 });
});

it('should overwrite existing properties', () => {
const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };
const result = objectAssign(target, source);

expect(result).toEqual({ a: 1, b: 3, c: 4 });
});

it('should handle null and undefined sources', () => {
const target = { a: 1 };
const result = objectAssign(target, null, undefined, { b: 2 });

expect(result).toEqual({ a: 1, b: 2 });
});

it('should handle primitive sources', () => {
const target = { a: 1 };
const result = objectAssign(target, 'string', 42, true, null);

expect(result).toEqual({ a: 1 });
});

it('should handle empty target and sources', () => {
const result = objectAssign({}, { a: 1 }, {});

expect(result).toEqual({ a: 1 });
});

it('should not copy non-enumerable properties', () => {
const source = {};
Object.defineProperty(source, 'nonEnumerable', {
value: 'test',
enumerable: false
});

const target = {};
const result = objectAssign(target, source);

expect(result).toEqual({});
expect('nonEnumerable' in result).toBe(false);
});

it('should handle symbol properties', () => {
const symbol = Symbol('test');
const target = {};
const source = { [symbol]: 'value', regular: 'prop' };
const result = objectAssign(target, source);

expect(result).toEqual({ regular: 'prop' });
expect(result[symbol]).toBe('value');
});
});
```