{"id":23041320,"url":"https://github.com/completejavascript/vanilla-js-snippets","last_synced_at":"2025-04-03T00:40:50.530Z","repository":{"id":93865001,"uuid":"163744918","full_name":"completejavascript/vanilla-js-snippets","owner":"completejavascript","description":"This project is moved to https://github.com/jscenter/vanilla-js-snippets","archived":false,"fork":false,"pushed_at":"2019-01-04T15:48:16.000Z","size":591,"stargazers_count":2,"open_issues_count":3,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-08T14:45:13.944Z","etag":null,"topics":["helpers","javascript","js","plain-js","snippets","tookit","vanilla-javascript","vanilla-js","vanilla-js-snippets"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/completejavascript.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2019-01-01T15:02:10.000Z","updated_at":"2020-04-24T08:22:59.000Z","dependencies_parsed_at":"2023-03-06T10:00:39.753Z","dependency_job_id":null,"html_url":"https://github.com/completejavascript/vanilla-js-snippets","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/completejavascript%2Fvanilla-js-snippets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/completejavascript%2Fvanilla-js-snippets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/completejavascript%2Fvanilla-js-snippets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/completejavascript%2Fvanilla-js-snippets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/completejavascript","download_url":"https://codeload.github.com/completejavascript/vanilla-js-snippets/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246916739,"owners_count":20854511,"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":["helpers","javascript","js","plain-js","snippets","tookit","vanilla-javascript","vanilla-js","vanilla-js-snippets"],"created_at":"2024-12-15T19:32:27.928Z","updated_at":"2025-04-03T00:40:50.501Z","avatar_url":"https://github.com/completejavascript.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Useful Vanilla JS Snippets\n\nThis is a compilation of vanilla js snippets for web developers. \n\n  * [Convert IPv4 to Decimal](#convert-ipv4-to-decimal)\n  * [Convert Decimal to IPv4](#convert-decimal-to-ipv4)\n\n## Convert IPv4 to Decimal\n\n```js\n/**\n * Convert IPv4 to Decimal\n * @param {String} ip - a String represents an IPv4\n * @returns a decimal if the input is valid. Otherwise, it returns -1.\n */\nfunction convertIPv4toDecimal(ip) {\n  // Simply validate input as IPv4\n  const ipv4RegEx = /^(\\d{1,3}\\.){3}(\\d{1,3})$/;\n  const valid = ipv4RegEx.test(ip);\n  if (!valid) {\n    return -1;\n  }\n\n  let ipDecimal = 0;\n  const octets = ip.split('.');\n\n  for (let i = 0; i \u003c 4; i++) {\n    const octet = Number(octets[i]);\n\n    // make sure each value is between 0 and 255\n    if (octet \u003e 255 || octet \u003c 0) {\n      return -1;\n    }\n\n    ipDecimal = ipDecimal * 256 + octet;\n  }\n\n  return ipDecimal;\n}\n```\n\n## Convert Decimal to IPv4\n\n```js\n/**\n * Convert Decimal to IPv4\n * @param {Number} decimal - a Number represents a Decimal\n * @returns an IPv4 as String if the input is valid. \n * Otherwise, it returns an empty string (\"\").\n */\nfunction convertDecimalToIPv4(decimal) {\n  if (typeof decimal !== \"number\" || \n      Number.isInteger(decimal) === false ||\n      decimal \u003c 0 || \n      decimal \u003e 4294967295\n  ) {\n    return \"\";\n  }\n\n  const p1 = decimal \u003e\u003e\u003e 24;\n  const p2 = (decimal \u0026 0x00ff0000) \u003e\u003e\u003e 16;\n  const p3 = (decimal \u0026 0x0000ff00) \u003e\u003e\u003e 8;\n  const p4 = (decimal \u0026 0x000000ff);\n\n  if (p1 \u003e 255 || p2 \u003e 255 || p3 \u003e 255 || p4 \u003e 255) {\n     return \"\";\n  }\n\n  return `${p1}.${p2}.${p3}.${p4}`;\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompletejavascript%2Fvanilla-js-snippets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcompletejavascript%2Fvanilla-js-snippets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcompletejavascript%2Fvanilla-js-snippets/lists"}