|
| 1 | +const {queue} = require('./index') |
| 2 | + |
| 3 | +test('Queue test 1', assert => { |
| 4 | + const patient = (name = 'generic', code = '0') => Object.assign({}, {name, code}) |
| 5 | + |
| 6 | + const ed = queue() |
| 7 | + const p = patient('Smith', 5) |
| 8 | + ed.enqueue(p) |
| 9 | + const p2 = patient('Jones', 4) |
| 10 | + ed.enqueue(p2) |
| 11 | + const p3 = patient('Fehrenbach', 6) |
| 12 | + ed.enqueue(p3) |
| 13 | + const p4 = patient('Brown', 1) |
| 14 | + ed.enqueue(p4) |
| 15 | + const p5 = patient('Ingram', 1) |
| 16 | + ed.enqueue(p5) |
| 17 | + |
| 18 | + const dequeue = function () { |
| 19 | + let priority = this.front().code |
| 20 | + let position = 0 |
| 21 | + let dataStore = this.getQueue() |
| 22 | + for (let i = 0; i < this.length(); i++) { |
| 23 | + if (dataStore[i].code >= priority) { |
| 24 | + priority = dataStore[i].code |
| 25 | + position = i |
| 26 | + } |
| 27 | + } |
| 28 | + return dataStore.splice(position, 1) |
| 29 | + } |
| 30 | + |
| 31 | + Object.assign(ed, {dequeue}) |
| 32 | + |
| 33 | + let seen = [] |
| 34 | + |
| 35 | + while (ed.length() > 0) { |
| 36 | + let {name} = ed.dequeue()[0] |
| 37 | + seen.push(name) |
| 38 | + // console.log('\nPatient being treated: ' + seen[seen.length - 1]) |
| 39 | + } |
| 40 | + |
| 41 | + assert.equal(seen[0], 'Fehrenbach', 'dequeue fn override and dequeue by code') |
| 42 | +}) |
| 43 | + |
| 44 | +test('Queue test 2', assert => { |
| 45 | + function isPalindrome (word) { |
| 46 | + const letter = new RegExp('[a-z]') |
| 47 | + let palindrome = word.toLowerCase().split('') |
| 48 | + let a, b |
| 49 | + palindrome = palindrome.filter(item => letter.test(item)) |
| 50 | + const q = queue(palindrome) |
| 51 | + |
| 52 | + const dequeueBack = function (element) { |
| 53 | + const dataStore = this.getQueue() |
| 54 | + const data = dataStore.unshift(element) |
| 55 | + this.setQueue(data) |
| 56 | + return data |
| 57 | + } |
| 58 | + |
| 59 | + Object.assign(q, {dequeueBack}) |
| 60 | + |
| 61 | + while (!q.isEmpty()) { |
| 62 | + if (q.length() > 1) { |
| 63 | + a = q.front() |
| 64 | + b = q.back() |
| 65 | + } else { |
| 66 | + // console.log(`The string '${word.toUpperCase()}' is palindrome!`) |
| 67 | + return true |
| 68 | + } |
| 69 | + if (a === b && q.length() > 1) { |
| 70 | + q.dequeue() |
| 71 | + q.dequeueBack() |
| 72 | + } else { |
| 73 | + // console.log(`The string '${word.toUpperCase()}' is not palindrome!`) |
| 74 | + return false |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + const word = 'racecar' |
| 80 | + const word2 = 'A man, a plan, a canal: Panama' |
| 81 | + const word3 = 'what is this' |
| 82 | + |
| 83 | + assert.equal(true, isPalindrome(word), `the string = '${word}' is palindrome`) |
| 84 | + assert.equal(true, isPalindrome(word2), `the string = '${word2}' is palindrome`) |
| 85 | + assert.equal(false, isPalindrome(word3), `the string = '${word3}' is not a palindrome`) |
| 86 | +}) |
0 commit comments