1.1.1 • Published 5 months ago

js-internals v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

Boost Up The Js Understanding ā˜ ļø

github-header-image (2)

Welcome to the Custom JavaScript Functions package! This npm package provides custom implementations of built-in JavaScript functions, allowing users to understand how these functions work internally. This is especially useful for learning purposes and for those who want to see how common JavaScript functions can be replicated.

Features šŸš€

  • Custom Implementations: Each function in this package is a custom implementation that mimics the behavior of the native JavaScript functions.
  • Educational: The primary goal of this package is to provide an educational resource for developers to understand the inner workings of JavaScript functions.
  • Polyfill-style Functions: These implementations can serve as polyfills, helping developers use similar functionality in environments where certain features may not be available.

example code 🐬

const { INTERNAL , GET_ALL } = require('js-internals');
//INTERNAL  is an object that contains all the custom native funcs
//map_internals() gives that implemented custom code of native js funcs in a seperate folder
//You can use this custom funcs just by providing a "$" symbol before the name eg. $map();

INTERNAL.map_internals() //You will get the custom implementation of map()

//sample example
const numbers = [1, 2, 3];
const filtered = numbers.$filter(n => n > 2);
console.log('Filtered:', filtered);
//output [3]

//Get all the custom implementations at once
GET_ALL()

Project Tree 🌳

šŸ“¦ js-internals
│
ā”œā”€ā”€ šŸ“‚ src
│   ā”œā”€ā”€ šŸ“‚ DataStructures
│   │   ā”œā”€ā”€ šŸ“„ Set.js
│   │
│   ā”œā”€ā”€ šŸ“‚ Polyfills
│   │   ā”œā”€ā”€ šŸ“‚ Array
│   │   │   ā”œā”€ā”€ šŸ“‚ InstanceMethods
│   │   │   │   ā”œā”€ā”€ šŸ“„ _map.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _filter.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _reduce.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _forEach.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _some.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _every.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _find.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _indexOf.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _includes.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _sort.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _reverse.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _splice.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _slice.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _concat.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _join.js
│   │   │   ā”œā”€ā”€ šŸ“‚ StaticMethods
│   │   │   │   ā”œā”€ā”€ šŸ“„ _from.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _isArray.js
│   │   │   │   ā”œā”€ā”€ šŸ“„ _of.js
│   │   │
│   │   ā”œā”€ā”€ šŸ“‚ String
│   │   │   ā”œā”€ā”€ šŸ“„ _charAt.js
│   │   │   ā”œā”€ā”€ šŸ“„ _indexOf.js
│   │   │   ā”œā”€ā”€ šŸ“„ _includes.js
│   │   │   ā”œā”€ā”€ šŸ“„ _slice.js
│   │   │   ā”œā”€ā”€ šŸ“„ _substring.js
│   │   │   ā”œā”€ā”€ šŸ“„ _trim.js
│   │   │   ā”œā”€ā”€ šŸ“„ _toLowerCase.js
│   │   │   ā”œā”€ā”€ šŸ“„ _toUpperCase.js
│   │   │   ā”œā”€ā”€ šŸ“„ _split.js
│   │   │   ā”œā”€ā”€ šŸ“„ _replace.js
│   │   │
│   │   ā”œā”€ā”€ šŸ“‚ Object
│   │   │   ā”œā”€ā”€ šŸ“„ _keys.js
│   │   │   ā”œā”€ā”€ šŸ“„ _values.js
│   │   │   ā”œā”€ā”€ šŸ“„ _entries.js
│   │   │   ā”œā”€ā”€ šŸ“„ _assign.js
│   │   │   ā”œā”€ā”€ šŸ“„ _freeze.js
│   │   │   ā”œā”€ā”€ šŸ“„ _seal.js
│   │   │   ā”œā”€ā”€ šŸ“„ _getOwnPropertyDescriptor.js
│   │   │   ā”œā”€ā”€ šŸ“„ _create.js
│   │   │   ā”œā”€ā”€ šŸ“„ _hasOwnProperty.js
│   │   │
│   │   ā”œā”€ā”€ šŸ“‚ Function
│   │   │   ā”œā”€ā”€ šŸ“„ _call.js
│   │   │   ā”œā”€ā”€ šŸ“„ _apply.js
│   │   │   ā”œā”€ā”€ šŸ“„ _bind.js
│   │   │
│   │   ā”œā”€ā”€ šŸ“‚ Math
│   │   │   ā”œā”€ā”€ šŸ“„ _max.js
│   │   │   ā”œā”€ā”€ šŸ“„ _min.js
│   │   │   ā”œā”€ā”€ šŸ“„ _floor.js
│   │   │   ā”œā”€ā”€ šŸ“„ _ceil.js
│   │   │   ā”œā”€ā”€ šŸ“„ _round.js
│   │   │   ā”œā”€ā”€ šŸ“„ _random.js
│   │   │   ā”œā”€ā”€ šŸ“„ _abs.js
│   │   │
│   │   ā”œā”€ā”€ šŸ“‚ Global
│   │   │   ā”œā”€ā”€ šŸ“„ _parseInt.js
│   │   │   ā”œā”€ā”€ šŸ“„ _parseFloat.js
│   │   │   ā”œā”€ā”€ šŸ“„ _isNaN.js
│   │   │   ā”œā”€ā”€ šŸ“„ _isFinite.js
│   │   │   ā”œā”€ā”€ šŸ“„ _stringify.js
│   │   │   ā”œā”€ā”€ šŸ“„ _parse.js
│   │
│   ā”œā”€ā”€ šŸ“„ index.js
│
ā”œā”€ā”€ šŸ“‚ tests
│   ā”œā”€ā”€ šŸ“‚ Array
│   │   ā”œā”€ā”€ šŸ“„ test_map.js
│   │   ā”œā”€ā”€ šŸ“„ test_filter.js
│   │   ā”œā”€ā”€ šŸ“„ test_reduce.js
│   │   ā”œā”€ā”€ šŸ“„ test_forEach.js
│   │   ā”œā”€ā”€ šŸ“„ ...
│   ā”œā”€ā”€ šŸ“‚ String
│   │   ā”œā”€ā”€ šŸ“„ test_charAt.js
│   │   ā”œā”€ā”€ šŸ“„ test_indexOf.js
│   │   ā”œā”€ā”€ šŸ“„ ...
│   ā”œā”€ā”€ šŸ“‚ Object
│   │   ā”œā”€ā”€ šŸ“„ test_keys.js
│   │   ā”œā”€ā”€ šŸ“„ test_values.js
│   │   ā”œā”€ā”€ šŸ“„ ...
│   ā”œā”€ā”€ šŸ“‚ Math
│   │   ā”œā”€ā”€ šŸ“„ test_max.js
│   │   ā”œā”€ā”€ šŸ“„ test_min.js
│   │   ā”œā”€ā”€ šŸ“„ ...
│   ā”œā”€ā”€ šŸ“‚ Global
│   │   ā”œā”€ā”€ šŸ“„ test_parseInt.js
│   │   ā”œā”€ā”€ šŸ“„ test_isNaN.js
│   │   ā”œā”€ā”€ šŸ“„ ...
│   ā”œā”€ā”€ šŸ“„ script.js
│
ā”œā”€ā”€ šŸ“„ package.json

Sample Code of custom $call(context, ...args) Same as native call()

/**
 * Function.prototype.call() - Calls a function with a given 'this' value and arguments provided individually.
 * The "call()" method allows you to invoke a function with a specific 'this' context, along with arguments passed individually. It is useful when you want to change the context ('this') for a particular function invocation.
 * @syntax :
 * @function.call(thisArg, arg1, arg2, ..., argN);

 * @thisArg : The value to use as 'this' when calling the function. If 'thisArg' is 'null' or 'undefined', it will default to the global object ('globalThis' in non-strict mode).
 * @arg1 , arg2, ..., argN: Individual arguments to pass to the function.

 * @returns:
 * The result of calling the function with the provided 'this' value and arguments.
**/

Function.prototype.__call = function(context, ...args) {
    if (typeof this !== "function") {
        throw new Error(this + " is not a function.");
    }

    if (context === undefined || context === null) {
        return this.apply(globalThis, args);
    }

    const boundContext = Object(context);

    boundContext.func = this;
    const result = boundContext.func(...args);
    delete boundContext.func;

    return result;
};

Progress ✨ ~

image

Available Functions šŸš€

Array Methods

FunctionStatusNative Function
$Map(array, callback)Implemented āœ…Array.prototype.map()
$Filter(array, callback)Implemented āœ…Array.prototype.filter()
$Reduce(array, callback, initialValue)Implemented āœ…Array.prototype.reduce()
$Array.from(value)Implemented āœ…Array.from()
$Array.isArray(array)Implemented āœ…Array.isArray()
$Array.of(value)Implemented āœ…Array.of()
$ForEach(array, callback)Implemented āœ…Array.prototype.forEach()
$Some(array, callback)Implemented āœ…Array.prototype.some()
$Every(array, callback)Implemented āœ…Array.prototype.every()
$Find(array, callback)TodoArray.prototype.find()
$IndexOf(array, element)TodoArray.prototype.indexOf()
$Includes(array, element)TodoArray.prototype.includes()
$Sort(array, compareFunction)TodoArray.prototype.sort()
$Reverse(array)TodoArray.prototype.reverse()
$Splice(array, start, deleteCount, ...items)TodoArray.prototype.splice()
$Slice(array, start, end)TodoArray.prototype.slice()
$Concat(array, ...arrays)TodoArray.prototype.concat()
$Join(array, separator)TodoArray.prototype.join()

String Methods

FunctionStatusNative Function
$CharAt(string, index)TodoString.prototype.charAt()
$IndexOf(string, searchValue, fromIndex)TodoString.prototype.indexOf()
$Includes(string, searchString, fromIndex)TodoString.prototype.includes()
$Slice(string, start, end)TodoString.prototype.slice()
$Substring(string, start, end)TodoString.prototype.substring()
$Trim(string)TodoString.prototype.trim()
$ToLowerCase(string)TodoString.prototype.toLowerCase()
$ToUpperCase(string)TodoString.prototype.toUpperCase()
$Split(string, separator, limit)TodoString.prototype.split()
$Replace(string, searchValue, newValue)TodoString.prototype.replace()

Object Methods

FunctionStatusNative Function
$Keys(object)TodoObject.keys()
$Values(object)TodoObject.values()
$Entries(object)TodoObject.entries()
$Assign(target, ...sources)TodoObject.assign()
$Freeze(object)TodoObject.freeze()
$Seal(object)TodoObject.seal()
$GetOwnPropertyDescriptor(object, property)TodoObject.getOwnPropertyDescriptor()
$Create(prototype, properties)TodoObject.create()
$HasOwnProperty(object, property)TodoObject.prototype.hasOwnProperty()

Function Methods

FunctionStatusNative Function
$Call(func, thisArg, ...args)Implemented āœ…Function.prototype.call()
$Apply(func, thisArg, args)Implemented āœ…Function.prototype.apply()
$Bind(func, thisArg, ...args)TodoFunction.prototype.bind()

Math Methods

FunctionStatusNative Function
$Max(...numbers)TodoMath.max()
$Min(...numbers)TodoMath.min()
$Floor(number)TodoMath.floor()
$Ceil(number)TodoMath.ceil()
$Round(number)TodoMath.round()
$Random()TodoMath.random()
$Abs(number)TodoMath.abs()

Other Global Functions

FunctionStatusNative Function
$ParseInt(string, radix)Implemented āœ…parseInt()
$ParseFloat(string)TodoparseFloat()
$IsNaN(value)Implemented āœ…isNaN()
$IsFinite(value)Implemented āœ…isFinite()
$Stringify(value)TodoJSON.stringify()
$Parse(jsonString)TodoJSON.parse()

Hello Devs šŸ‘‹šŸ» If you want to contribute you are always Welcome !! Come and Contribute Here > 3 šŸ’—šŸ« 

Contribution Rule's

  • maintain existing file naming convension (eg. _file.js)
  • maintain existing code format for Contribution
  • ensure you write all the import tests for that func maintaining the proper format (eg. test_funcName.js)
  • demo test file for custom func $call()
const { __call } = require('../package/src/internal-custom-functions/Array_Object/Static_Methods/_call');

function deepEqual(obj1, obj2) {
    return JSON.stringify(obj1) === JSON.stringify(obj2);
}

// Run tests for the custom __call method
const tests = [
    {
        description: 'Basic Function Call with Context',
        func: function(greeting, punctuation) { return `${greeting}, ${this.name}${punctuation}`; },
        context: { name: 'Alice' },
        args: ['Hello', '!'],
        expected: 'Hello, Alice!',
        testNumber: 0
    },
    {
        description: 'Function with No Context',
        func: function(num1, num2) { return num1 + num2; },
        context: undefined,
        args: [5, 10],
        expected: 15,
        testNumber: 1
    }
    //Write atleast 7-8 proper test cases.
];

// Run the tests
tests.forEach(test => {
    let result;
    let errorOccurred = false;
    try {
        result = test.func.__call(test.context, ...test.args);
    } catch (error) {
        if (test.expectedError) {
            errorOccurred = true;
            console.log(`Passed Test${test.testNumber} āœ…: ${test.description}`);
        } else {
            console.log(`Failed Test${test.testNumber} āŒ: ${test.description}`);
            console.error('Unexpected Error:', error);
        }
    }

    if (!errorOccurred && deepEqual(result, test.expected)) {
        console.log(`Passed Test${test.testNumber} āœ…: ${test.description}`);
    } else if (!errorOccurred) {
        console.log(`Failed Test${test.testNumber} āŒ: ${test.description}`);
        console.log(`Expected: ${JSON.stringify(test.expected)}`);
        console.log(`Actual: ${JSON.stringify(result)}`);
    }
});

Project SetUp

First Clone The Repo

git clone https://github.com/Rudrajiii/Js-Internals

Then Move To Project Directory

cd Js-Internals

Now Open Your Favourite Code Editor Mine is Vs-Code :) So

code .

Installation ā¬

To install this package, use npm:

npm install --save-dev js-internals

To Run The Tests: Make the test script executable: For (Linux):

chmod +x pkg_tester.sh

Run the test script:

./pkg_tester.sh
1.1.1

5 months ago

1.1.0

5 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

7 months ago

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago