Skip to content

Commit

Permalink
Add getSort functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
borota committed Nov 13, 2017
1 parent 0a01794 commit 184c35e
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ npm run build
* [.map(word, wordProps)](#module_aramaicMapper.Mapper+map) ⇒ <code>string</code>
* [.hasDotting(isDotting)](#module_aramaicMapper.hasDotting) ⇒ <code>function</code>
* [.clearDotting(isDotting)](#module_aramaicMapper.clearDotting) ⇒ <code>function</code>
* [.getSort(letterAsciiMap, removeDotting)](#module_aramaicMapper.getSort) ⇒ <code>function</code>
* [.mapCallback](#module_aramaicMapper.mapCallback) ⇒ <code>string</code>

<a name="module_aramaicMapper.Writing"></a>
Expand Down Expand Up @@ -199,6 +200,19 @@ skeleton only.
| --- | --- | --- |
| isDotting | <code>function</code> | (char => boolean) which checks if char is dotting |

<a name="module_aramaicMapper.getSort"></a>

### aramaicMapper.getSort(letterAsciiMap, removeDotting) ⇒ <code>function</code>
Returns a function to be used for sorting words using the provided `letterAsciiMap`

**Kind**: static method of [<code>aramaicMapper</code>](#module_aramaicMapper)
**Returns**: <code>function</code> - ((word1, word2) => number) function implementation

| Param | Type | Description |
| --- | --- | --- |
| letterAsciiMap | <code>Object.&lt;string, string&gt;</code> | letter to ASCII value map |
| removeDotting | <code>function</code> | (word => word) remove dots function |

<a name="module_aramaicMapper.mapCallback"></a>

### aramaicMapper.mapCallback ⇒ <code>string</code>
Expand Down
51 changes: 50 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,53 @@ const clearDotting = isDotting => word => {
return hasDots ? stack.join('') : word;
};

export { Writing, Mapper, hasDotting, clearDotting };
/**
* Convert word to ASCII sort chars
* @private
* @static
* @param { string } word input word
* @param { Object.<string, string> } letterAsciiMap letter to ASCII value map
* @returns { string } word converted as ASCII sort
*/
const toAsciiSort = (word, letterAsciiMap) => {
let sb = '';
for (let i = 0, len = word.length; i < len; i++) {
const c = word.charAt(i);
const m = letterAsciiMap[c];
sb += m || (m === '' ? '' : c);
}
return sb;
};

/**
* Returns a function to be used for sorting words using the provided `letterAsciiMap`
* @static
* @param { Object.<string, string> } letterAsciiMap letter to ASCII value map
* @param { function } removeDotting (word => word) remove dots function
* @returns { function } ((word1, word2) => number) function implementation
*/
const getSort = (letterAsciiMap, removeDotting) => (word1, word2) => {
if (!word1 || !word2) {
return !word1 && !word2 ? 0 : !word1 ? -1 : 1;
}

const cons1 = removeDotting(word1);
const cons2 = removeDotting(word2);
let asc1 = toAsciiSort(cons1, letterAsciiMap);
let asc2 = toAsciiSort(cons2, letterAsciiMap);
if (asc1 < asc2) {
return -1;
}
if (asc1 > asc2) {
return 1;
}

if (cons1 === word1 && cons2 === word2) {
return 0; // no dots
}
asc1 = toAsciiSort(word1, letterAsciiMap);
asc2 = toAsciiSort(word2, letterAsciiMap);
return asc1 < asc2 ? -1 : asc1 > asc2 ? 1 : 0;
};

export { Writing, Mapper, hasDotting, clearDotting, getSort };
107 changes: 106 additions & 1 deletion test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const {
Writing,
Mapper,
clearDotting,
hasDotting
hasDotting,
getSort
} = require('../build/aramaic-mapper');

const consonants = [
Expand Down Expand Up @@ -64,6 +65,55 @@ const toCalWriting = new Writing(
diacritics
);

/**
* Letter ordinal value. Used for sorting:
* a b c d e f g h i j k l m n o p q r s t u v - A O E I U
* @constant
* @type { Object.<string, string> }
*/
const letterAsciiMap = Object.freeze(
Object.create(null, {
A: { value: 'a', enumerable: true },
B: { value: 'b', enumerable: true },
G: { value: 'c', enumerable: true },
D: { value: 'd', enumerable: true },

H: { value: 'e', enumerable: true },
O: { value: 'f', enumerable: true },
Z: { value: 'g', enumerable: true },

K: { value: 'h', enumerable: true },
Y: { value: 'i', enumerable: true },
';': { value: 'j', enumerable: true },

C: { value: 'k', enumerable: true },
L: { value: 'l', enumerable: true },
M: { value: 'm', enumerable: true },
N: { value: 'n', enumerable: true },

S: { value: 'o', enumerable: true },
E: { value: 'p', enumerable: true },
I: { value: 'q', enumerable: true },
'/': { value: 'r', enumerable: true },

X: { value: 's', enumerable: true },
R: { value: 't', enumerable: true },
W: { value: 'u', enumerable: true },
T: { value: 'v', enumerable: true },

a: { value: 'w', enumerable: true },
o: { value: 'x', enumerable: true },
e: { value: 'y', enumerable: true },
i: { value: 'z', enumerable: true },
u: { value: '{', enumerable: true },

"'": { value: '', enumerable: true },
',': { value: ',', enumerable: true },
_: { value: '', enumerable: true },
'*': { value: '', enumerable: true }
})
);

const isDotting = c => vowels.concat(diacritics).indexOf(c) > -1;

describe('Sedra', () => {
Expand Down Expand Up @@ -550,3 +600,58 @@ describe('Sedra', () => {
});
});
});

describe('Sedra', () => {
const removeDotting = clearDotting(isDotting);
const sort = getSort(letterAsciiMap, removeDotting);

describe('getSort', () => {
it('(null, word)', () => {
const nullWord = null;
const word = 'DXSR;A-DI;L;IOS';
const expected = sort(nullWord, word);
strictEqual(-1, expected, 'getSort (null, word)');
});
it('(word, null)', () => {
const word = 'DXSR;A-DI;L;IOS';
const nullWord = null;
const expected = sort(word, nullWord);
strictEqual(1, expected, 'getSort (word, null)');
});
it('(null, null)', () => {
const word = null;
const nullWord = null;
const expected = sort(word, nullWord);
strictEqual(0, expected, 'getSort (null, null)');
});
it('(word, wordVocalized)', () => {
const word = 'DXSR;A-DI;L;IOS';
const vocalised = "D'XeSaRi;aA-D,I,i;Li;I'oOS";
const expected = sort(word, vocalised);
strictEqual(-1, expected, 'getSort vocalised');
});
it('Consonant only great sort', () => {
const word1 = 'LBELDBB;CON';
const word2 = 'DXSR;A-DI;L;IOS';
const expected = sort(word1, word2);
strictEqual(1, expected, 'getSort');
});
it('Consonant only less sort', () => {
const word1 = 'DXSR;A-DI;L;IOS';
const word2 = 'LBELDBB;CON';
const expected = sort(word1, word2);
strictEqual(-1, expected, 'getSort');
});
it('Consonant only equal sort', () => {
const word1 = 'DXSR;A-DI;L;IOS';
const word2 = 'DXSR;A-DI;L;IOS';
const expected = sort(word1, word2);
strictEqual(0, expected, 'getSort');
});
it('Blank word returns blank', () => {
const word = sort('');
const wordExpected = '';
strictEqual(word, sort(0, wordExpected), 'getSort_blank');
});
});
});

0 comments on commit 184c35e

Please sign in to comment.