Skip to content

Commit

Permalink
feat: eslint & format
Browse files Browse the repository at this point in the history
  • Loading branch information
selmi-karim committed May 30, 2020
1 parent 8a0e230 commit 376b30f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 49 deletions.
77 changes: 40 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,56 @@
const twoStrings = (sa1, sa2) => {
// Compare two strings to see how similar they are.
// Answer is returned as a value from 0 - 1
// 1 indicates a perfect similarity (100%) while 0 indicates no similarity (0%)
// The algorithm is an n-gram comparison of bigrams of characters in a string
// Compare two strings to see how similar they are.
// Answer is returned as a value from 0 - 1
// 1 indicates a perfect similarity (100%) while 0 indicates no similarity (0%)
// The algorithm is an n-gram comparison of bigrams of characters in a string

// Comparison should not check case or whitespace
let s1 = sa1.replace(/\s/g, "").toLowerCase();
let s2 = sa2.replace(/\s/g, "").toLowerCase();
// Comparison should not check case or whitespace
let s1 = sa1.replace(/\s/g, "").toLowerCase();
let s2 = sa2.replace(/\s/g, "").toLowerCase();

const similarity_num = 2 * intersect(pairs(s1), pairs(s2)).length;
const similarity_den = pairs(s1).length + pairs(s2).length;
return similarity_num / similarity_den;
const similarity_num = 2 * intersect(pairs(s1), pairs(s2)).length;
const similarity_den = pairs(s1).length + pairs(s2).length;
return similarity_num / similarity_den;
};

// private functions ---------------------------
const intersect = (arr1, arr2) => {
let r = [], o = {}, l = arr2.length, i, v;
for (i = 0; i < l; i++) {
o[arr2[i]] = true;
let r = [],
o = {},
l = arr2.length,
i,
v;
for (i = 0; i < l; i++) {
o[arr2[i]] = true;
}
l = arr1.length;
for (i = 0; i < l; i++) {
v = arr1[i];
if (v in o) {
r.push(v);
}
l = arr1.length;
for (i = 0; i < l; i++) {
v = arr1[i];
if (v in o) {
r.push(v);
}
}
return r;
}
}
return r;
};

// private functions ---------------------------
const pairs = (s) => {
// Get an array of all pairs of adjacent letters in a string
let pairs = [];
for (let i = 0; i < s.length - 1; i++) {
pairs[i] = s.slice(i, i + 2);
}
return pairs;
}
// Get an array of all pairs of adjacent letters in a string
let pairs = [];
for (let i = 0; i < s.length - 1; i++) {
pairs[i] = s.slice(i, i + 2);
}
return pairs;
};

// Sort array with dice Comparator
const simSort = (s1,arr1) => {
// Comparison should not check case or whitespace
return arr1.sort(function(a,b) {
let v1 = twoStrings(s1,a)
let v2 = twoStrings(s1,b);
return v2-v1
});

const simSort = (s1, arr1) => {
// Comparison should not check case or whitespace
return arr1.sort(function (a, b) {
let v1 = twoStrings(s1, a);
let v2 = twoStrings(s1, b);
return v2 - v1;
});
};

exports.twoStrings = twoStrings;
Expand Down
21 changes: 9 additions & 12 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
const dsc = require('./index');

const dsc = require("./index");

/** Comparing 2 exact same strings returns 1 */
test('matching two equal strings', () => {
expect(dsc.twoStrings('hello world', 'hello world')).toBe(1);
test("matching two equal strings", () => {
expect(dsc.twoStrings("hello world", "hello world")).toBe(1);
});


/** Comparing 2 different strings returns ]0,1[ */
test('matching two different strings', () => {
expect(dsc.twoStrings('hello world', 'hello world!')).toBeGreaterThan(0.9);
expect(dsc.twoStrings('hello world', 'Hello es6')).toBeGreaterThan(0.4);
test("matching two different strings", () => {
expect(dsc.twoStrings("hello world", "hello world!")).toBeGreaterThan(0.9);
expect(dsc.twoStrings("hello world", "Hello es6")).toBeGreaterThan(0.4);
});


/** Comparing 2 two completely different strings returns 0 */
test('matching two different strings', () => {
expect(dsc.twoStrings('hello world', 'es6')).toBe(0);
});
test("matching two different strings", () => {
expect(dsc.twoStrings("hello world", "es6")).toBe(0);
});

0 comments on commit 376b30f

Please sign in to comment.