UI for Zipcoin Blue

levenshtein.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. Copyright (c) 2011 Andrei Mackenzie
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  4. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  5. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  6. */
  7. // levenshtein distance algorithm, pulled from Andrei Mackenzie's MIT licensed.
  8. // gist, which can be found here: https://gist.github.com/andrei-m/982927
  9. // Compute the edit distance between the two given strings
  10. module.exports = function (a, b) {
  11. if (a.length === 0) return b.length
  12. if (b.length === 0) return a.length
  13. var matrix = []
  14. // increment along the first column of each row
  15. var i
  16. for (i = 0; i <= b.length; i++) {
  17. matrix[i] = [i]
  18. }
  19. // increment each column in the first row
  20. var j
  21. for (j = 0; j <= a.length; j++) {
  22. matrix[0][j] = j
  23. }
  24. // Fill in the rest of the matrix
  25. for (i = 1; i <= b.length; i++) {
  26. for (j = 1; j <= a.length; j++) {
  27. if (b.charAt(i - 1) === a.charAt(j - 1)) {
  28. matrix[i][j] = matrix[i - 1][j - 1]
  29. } else {
  30. matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, // substitution
  31. Math.min(matrix[i][j - 1] + 1, // insertion
  32. matrix[i - 1][j] + 1)) // deletion
  33. }
  34. }
  35. }
  36. return matrix[b.length][a.length]
  37. }