Polyfills Cont...
Tags: webdev, javascript, programming, polyfill
Published: 2023-01-20 07:20:54

***Implement the pipe() polyfill***

```javascript
if (!Function.prototype.pipe) {
  Function.prototype.pipe = function (fn) {
    return (...args) => fn(this(...args));
  };
}
```
You can use it like this:
```javascript
const add = (a, b) => a + b;
const double = (x) => x * 2;
const addThenDouble = add.pipe(double);
console.log(addThenDouble(1, 2)) //6
```

***Implement the publisher-subscriber pattern in javascript***
```javascript
class EventEmitter {
  constructor() {
    this.subscribers = new Map();
  }

  subscribe(event, callback) {
    if (!this.subscribers.has(event)) {
      this.subscribers.set(event, []);
    }
    this.subscribers.get(event).push(callback);
  }

  unsubscribe(event, callback) {
    if (this.subscribers.has(event)) {
      this.subscribers.set(event, this.subscribers.get(event).filter((cb) => cb !== callback));
    }
  }

  emit(event, data) {
    if (this.subscribers.has(event)) {
      this.subscribers.get(event).forEach((callback) => callback(data));
    }
  }
}
```
You can use it like this:
```javascript
const eventEmitter = new EventEmitter();

const callback1 = (data) => console.log("Event 1:", data);
const callback2 = (data) => console.log("Event 2:", data);

eventEmitter.subscribe("event1", callback1);
eventEmitter.subscribe("event2", callback2);

eventEmitter.emit("event1", { message: "Hello World!" });
// Output: "Event 1: { message: 'Hello World!' }"

eventEmitter.emit("event2", { message: "Hello World!" });
// Output: "Event 2: { message: 'Hello World!' }"

eventEmitter.unsubscribe("event1", callback1);
//unsubscribe from an event
```

***Implement the Array.map method on the Array Prototype***

```javascript
Array.prototype.map = function(callback) {
    var newArr = [];
    for (var i = 0; i < this.length; i++) {
        newArr.push(callback(this[i], i, this));
    }
    return newArr;
};
```
*** Implement the Array.filter method on the Array Prototype***
```javascript
Array.prototype.filter = function(callback) {
    var newArr = [];
    for (var i = 0; i < this.length; i++) {
        if (callback(this[i], i, this)) {
            newArr.push(this[i]);
        }
    }
    return newArr;
};
```
***Implement the Array.reduce method on the Array Prototype***
```javascript
Array.prototype.reduce = function(callback, initialValue) {
    var accumulator = initialValue !== undefined ? initialValue : this[0];
    for (var i = initialValue !== undefined ? 0 : 1; i < this.length; i++) {
        accumulator = callback(accumulator, this[i], i, this);
    }
    return accumulator;
};
```
***Implement the Array.forEach method on the Array Prototype***
```javascript
Array.prototype.forEach = function(callback) {
    for (var i = 0; i < this.length; i++) {
        callback(this[i], i, this);
    }
};
```
***Implement the Function.bind method on the Function Prototype***

```javascript
if (!Function.prototype.bind) {
  Function.prototype.bind = function(context) {
    var self = this;
    var args = Array.prototype.slice.call(arguments, 1);

    return function() {
      var innerArgs = Array.prototype.slice.call(arguments);
      var finalArgs = args.concat(innerArgs);
      return self.apply(context, finalArgs);
    };
  };
}
```