{"id":20832047,"url":"https://github.com/amitmerchant1990/essential-vanilla-javascript-functions","last_synced_at":"2026-02-04T04:35:14.792Z","repository":{"id":68852944,"uuid":"104075729","full_name":"amitmerchant1990/essential-vanilla-javascript-functions","owner":"amitmerchant1990","description":"Essential Vanilla JavaScript Functions","archived":false,"fork":false,"pushed_at":"2019-03-29T04:18:58.000Z","size":15,"stargazers_count":60,"open_issues_count":1,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-12T08:32:47.524Z","etag":null,"topics":["functions","utility-function","vanilla-javascript"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amitmerchant1990.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-19T13:04:05.000Z","updated_at":"2024-08-22T04:16:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"3d2e2e1d-aab2-4997-8536-c4921a147705","html_url":"https://github.com/amitmerchant1990/essential-vanilla-javascript-functions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/amitmerchant1990/essential-vanilla-javascript-functions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amitmerchant1990%2Fessential-vanilla-javascript-functions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amitmerchant1990%2Fessential-vanilla-javascript-functions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amitmerchant1990%2Fessential-vanilla-javascript-functions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amitmerchant1990%2Fessential-vanilla-javascript-functions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amitmerchant1990","download_url":"https://codeload.github.com/amitmerchant1990/essential-vanilla-javascript-functions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amitmerchant1990%2Fessential-vanilla-javascript-functions/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263108752,"owners_count":23415001,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functions","utility-function","vanilla-javascript"],"created_at":"2024-11-18T00:09:58.443Z","updated_at":"2026-02-04T04:35:14.755Z","avatar_url":"https://github.com/amitmerchant1990.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Essential Vanilla JavaScript Functions\n\u003e Some of the missing functions implementation in JavaScript in vanilla form (inspired by [PHP](https://en.wikipedia.org/wiki/PHP)).\n\n- Arrays\n    - [`array_unique()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#array_unique) - Remove duplicates from an array\n    - [`array_merge()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#array_merge) - Merge two arrays\n    - [`array_chunk()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#array_chunk) - Splits an array into chunks of arrays\n    - [`array_collapse()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#array_collapse) - Collapses a collection of arrays into a single, flat array\n    - [`array_diff()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#array_diff) - Returns the values in the array1 that are not present in array2\n    - [`array_intersect()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#array_intersect) - Returns the values common in the two supplied arrays\n    - [`array_map()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#array_map) - Sends each value of an array to a user-made function, which returns new values\n    - [`array_reject()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#array_reject) - Filters the array using the given callback. The callback should return true if the item should be removed from the resulting array\n    - [`array_split()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#array_split) - Breaks an array into the given number of groups\n    - [`array_take()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#array_take) - Returns a new array with the specified number of items\n    - [`array_pad()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#array_pad) - Inserts a specified number of items, with a specified value, to an array\n    - [`range()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#range) - Creates an array containing a range of elements\n    \n- String\n    - [`chunk_split()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#chunk_split) - Splits a string into a series of smaller parts\n    - [`str_pad()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#str_pad) - Pads a string to a new length\n    - [`strrev()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#strrev) - Reverses a string\n    - [`similar_text()`](https://github.com/amitmerchant1990/vanilla-javascript-functions#similar_text) - Calculates the similarity between two strings\n\n## `array_unique()`\n\u003e Remove duplicates from an array\n```js\nfunction array_unique(arr){\n    var seen = {};\n    var ret_arr = [];\n    var key;\n    var i;\n\n    function keyify(obj){\n        var ret = \"\";\n        var j;\n\n        if (Object.prototype.toString.call(obj) === \"[object Object]\" || Object.prototype.toString.call(obj) === \"[object Array]\"){\n            for (j in obj){\n                ret += \"~\" + j + \"^\" + keyify(obj[j]) + \"%\";\n            }\n            return ret;\n        }else{\n          return obj;\n        }\n    }\n\n    for(i = 0; i \u003c arr.length; i++){\n        key = keyify(arr[i]);\n        if(!(key in seen)){\n            ret_arr.push(arr[i]);\n            seen[key] = true;\n        }\n    }\n\n    return ret_arr;\n}\n\narray_unique([4,5,4,6,7,8,2,6]);\n// [4, 5, 6, 7, 8, 2]\narray_unique([{a: 'val'}, {b: 'val', c: 'val'}, 'a', 'b', [1,2,3,4], {a: 'val'}, [1,2,3,4], [{a: 'val'}, {b: 'val'}], [{a: 'val'}, {b: 'val'}]]);\n// [{a: 'val'}, {b: 'val', c: 'val'}, 'a', 'b', [1,2,3,4], [{a: 'val'}, {b: 'val'}]]\n```\n\n## `array_merge()`\n\u003e Merge two arrays\n```js\nfunction array_merge(arr1, arr2){\n    for(var i=0; i\u003carr2.length; i++){\n        arr1.push(arr2[i]);\n    }\n    \n    return arr1;\n}\n\narray_merge([1, 2, 3], [4, 5]);\n// [1, 2, 3, 4, 5]\n```\n\n## `array_chunk()`\n\u003e Splits an array into chunks of arrays\n```js\nfunction array_chunk(arr, count){\n    var temp_arr = [];\n    \n    for(var i=0; i\u003carr.length;){\n        var chunk_arr = [];\n        for(var j=0; j\u003ccount; j++){\n            if(!arr[i])\n                break;\n            chunk_arr.push(arr[i]);\n            i++;\n        }\n        temp_arr.push(chunk_arr);\n    }\n    \n    return temp_arr;\n}\n\narray_chunk([1,2,3,4,5,6,7,8,9], 4);\n// [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9 ] ]\n```\n\n## `array_collapse()`\n\u003e Collapses a collection of arrays into a single, flat array\n```js\n\nfunction array_collapse(...arrays){\n    var collapse_arr = [];\n    \n    for(var i=0; i\u003carrays.length;i++){\n        for(var j=0; j\u003carrays[i].length; j++){\n            collapse_arr.push(arrays[i][j]);\n        }\n    }\n    \n    return collapse_arr;\n}\n\narray_collapse([1, 2, 3, 4], [5, 6], [\"hello\", \"world\"]);\n// [ 1, 2, 3, 4, 5, 6, 'hello', 'world' ]\n```\n\n## `array_diff()`\n\u003e Returns the values in the `arr1` that are not present in `arr2`\n```js\nfunction array_diff(arr1, arr2){\n    var temp_arr = [];\n    \n    for(var i=0; i\u003carr1.length; i++){\n        if(arr2.indexOf(arr1[i]) == -1){  \n            temp_arr.push(arr1[i]);\n        }\n    }\n    \n    return temp_arr;\n}\n\narray_diff([4,5,6,7, \"unicorn\"], [5, 6, 7]);\n// [ 4, 'unicorn' ]\n```\n\n## `array_intersect()`\n\u003e Returns the values common in the two supplied arrays\n```js\nfunction array_intersect(arr1, arr2){\n    var temp_arr = [];\n    \n    for(var i=0; i\u003carr1.length; i++){\n        if(arr2.indexOf(arr1[i]) != -1){  \n            temp_arr.push(arr1[i]);\n        }\n    }\n    \n    return temp_arr;\n}\n\narray_intersect([4,5,6,7, \"unicorn\"], [5, 6, 7, 8]);\n// [ 5, 6, 7 ]\n```\n\n## `array_map()`\n\u003e Sends each value of an array to a user-made function, which returns new values\n```js\nfunction array_map(arr, func){\n    var temp_arr = [];\n    \n    if(typeof func !== \"function\")\n        throw \"Second parameter should be a function\";\n    \n    for(var i=0; i\u003carr.length; i++){\n        temp_arr.push(func(arr[i]));\n    }\n    \n    return temp_arr;\n}\n\narray_map([1, 2, 3, 4, 5], function (value) {\n    return value * 2;\n});\n// [ 2, 4, 6, 8, 10 ]\n```\n\n## `array_reject()`\n\u003e  Filters the array using the given callback. The callback should return `true` if the item should be removed from the resulting array\n```js\nfunction array_reject(arr, func){\n    var temp_arr = [];\n    \n    if(typeof func !== \"function\")\n        throw \"Second parameter should be a function\";\n    \n    for(var i=0; i\u003carr.length; i++){\n        if(func(arr[i]))\n            temp_arr.push(arr[i]);\n    }\n    \n    return temp_arr;\n}\n\narray_reject([1, 2, 3, 4, 5], function (value) {\n    return value \u003e 3;\n});\n// [ 4, 5 ]\n```\n\n## `array_split()`\n\u003e Breaks an array into the given number of groups\n```js\nfunction array_split(arr, count){\n    var temp_arr = [];\n    var arr_length = arr.length;\n    \n    var chunk = Math.floor(arr_length/count);\n    \n    for(var i=0; i\u003carr.length;){\n        var chunk_arr = [];\n        \n        if(temp_arr.length == (count-1))\n            chunk = chunk + (arr_length-i);\n        \n        for(var j=0; j\u003cchunk; j++){\n            if(!arr[i])\n                break;\n            chunk_arr.push(arr[i]);\n            i++;\n        }\n        \n        temp_arr.push(chunk_arr);\n    }\n    \n    return temp_arr;\n}\n\narray_split([1,2,3,4,5,6,7,8,9], 4);\n// [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8, 9 ] ]\n```\n\n## `array_take()`\n\u003e Returns a new array with the specified number of items\n```js\nfunction array_take(arr, count){\n    var temp_arr = [];\n    \n    if(count\u003c0){\n        count = Math.abs(count);\n        for(var i=(arr.length-count); i\u003carr.length; i++){\n            temp_arr.push(arr[i]);\n        }\n    }else{\n        for(var i=0; i\u003ccount; i++){\n            temp_arr.push(arr[i]);\n        }\n    }\n    \n    return temp_arr;\n}\n\narray_take([1,2,3,4,5,6,7,8,9], 4);\n// [ 1, 2, 3, 4 ]\n```\n\u003e You may also pass a negative integer to take the specified amount of items from the end of the array:\n```js\narray_take([1,2,3,4,5,6,7,8,9], -3);\n// [ 7, 8, 9 ]\n```\n\n## `array_pad()`\n\u003e Inserts a specified number of items, with a specified value, to an array\n```js\n\nfunction array_pad(arr, size, value){\n    for(var i=0; i\u003csize; i++){\n        arr.push(value);\n    }\n    return arr;\n}\n\narray_pad([1,2,3,4], 2, \"unicorn\");\n// [ 1, 2, 3, 4, 'unicorn', 'unicorn' ]\n```\n\n## `range()`\n\u003e Creates an array containing a range of elements\n```js\nfunction range(start, end){\n    var temp_arr = [];\n    \n    for(var i=start; i\u003c=end; i++){\n        temp_arr.push(i);\n    }\n    \n    return temp_arr;\n}\n\nrange(5, 11);\n// [ 5, 6, 7, 8, 9, 10, 11 ]\n```\n\n## `chunk_split()`\n\u003e Splits a string into a series of smaller parts\n```js\nfunction chunk_split(string, length, end){\n    var temp_string = '';\n    \n    for(var i=0; i\u003cstring.length; i++){\n        temp_string += string[i];\n        if((i+1)%length==0)\n            temp_string += end;\n    }\n    \n    return temp_string;\n}\n\nconsole.log(chunk_split(\"Hello\", 1 , \".\"));\n// H.e.l.l.o.\n```\n\n## `str_pad()`\n\u003e Pads a string to a new length\n```js\nfunction str_pad(string, size, value){\n    for(var i=0; i\u003csize; i++){\n        string += value;\n    }\n    \n    return string;\n}\n\nstr_pad(\"unicorn\", 5 , \".\");\n// unicorn.....\n```\n\n## `strrev()`\n\u003e Reverses a string\n```js\nfunction strrev(string){\n    var temp_string = '';\n    \n    for(var i=string.length-1; i\u003e=0; i--){\n        temp_string += string[i];\n    }\n    \n    return temp_string;\n}\n\nstrrev(\"unicorn\");\n// nrocinu\n```\n\n## `similar_text()`\n\u003e Calculates the similarity between two strings\n```js\nfunction similar_text(string1, string2){\n    var seen = {};\n    var similar_count = 0;\n    \n    for(var i=0; i\u003cstring1.length; i++){\n        if((string2.indexOf(string1[i]) !== -1 \u0026\u0026 !(string1[i] in seen)) \n                || string1[i]==' ')\n        {\n            similar_count++;\n            if(string1[i]!='')\n                seen[string1[i]] = true;\n        }\n    }\n    \n    return similar_count;\n}\n\nsimilar_text(\"Hello World\",\"Hello Peter\");\n// 6\n```\n\n# License\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famitmerchant1990%2Fessential-vanilla-javascript-functions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famitmerchant1990%2Fessential-vanilla-javascript-functions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famitmerchant1990%2Fessential-vanilla-javascript-functions/lists"}