willyb321

find-unused-id.js

Apr 20th, 2018
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const data = require('./coriolis-data/dist/index.json')
  2.  
  3. function combinations(arr, k) {
  4.   let i
  5.   let subI
  6.   const ret = []
  7.   let sub
  8.   let next
  9.   for (i = 0; i < arr.length; i++) {
  10.     if (k === 1) {
  11.       ret.push([arr[i]])
  12.     } else {
  13.       sub = combinations(arr.slice(i + 1, arr.length), k - 1)
  14.       for (subI = 0; subI < sub.length; subI++) {
  15.         next = sub[subI]
  16.         next.unshift(arr[i])
  17.         ret.push(next)
  18.       }
  19.     }
  20.   }
  21.   return ret
  22. }
  23.  
  24. const alphabet = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'T', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
  25. const possibleIds = combinations(alphabet, 2)
  26. console.log(`Possible IDs: ${possibleIds.length}`)
  27. const list = []
  28.  
  29. function getId(arr) {
  30.   if (typeof arr === 'object') {
  31.     for (const i in arr) {
  32.       if (typeof arr[i] === 'object' && !arr[i].id) {
  33.         getId(arr[i])
  34.       } else if (arr[i].id) {
  35.         list.push(arr[i].id)
  36.       }
  37.     }
  38.   }
  39. }
  40.  
  41. getId(data)
  42. possibleIds.forEach((elem, ind) => possibleIds[ind] = elem.join(''))
  43. for (const i of possibleIds) {
  44.   if (list.indexOf(i) === -1) {
  45.     console.log(`Unused ID: ${i}`)
  46.     break
  47.   }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment