-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
CssSelectors, | ||
CssSelectorsByType, | ||
CssSelectorType, | ||
CssSelectorTypes | ||
} from './types'; | ||
import {SELECTOR_TYPE_GETTERS} from './utilities-selectors'; | ||
|
||
type MemoSelectorData = Map<CssSelectorType, CssSelectors> | ||
type MemoElementData = Map<Element, MemoSelectorData> | ||
|
||
/** | ||
* Creates interface for getting CSS selectors by type for an element. Results are remembered for use in later calls. | ||
*/ | ||
export function createMemo (memo: MemoElementData = new Map()) { | ||
|
||
function getElementData (element: Element): MemoSelectorData { | ||
if (!memo.get(element)) { | ||
memo.set(element, new Map()); | ||
} | ||
return memo.get(element); | ||
} | ||
|
||
function getSelectorData (element: Element, selectorType: CssSelectorType) { | ||
const elementData = getElementData(element); | ||
if (!elementData.get(selectorType)) { | ||
elementData.set(selectorType, getSelectors(element, selectorType)); | ||
} | ||
return elementData.get(selectorType); | ||
} | ||
|
||
function getSelectors (element: Element, selectorType: CssSelectorType): CssSelectors { | ||
return SELECTOR_TYPE_GETTERS[selectorType](element); | ||
} | ||
|
||
return function (element: Element, selectors: CssSelectorTypes): CssSelectorsByType { | ||
const result = {} as CssSelectorsByType; | ||
selectors.forEach((selectorType) => { | ||
result[selectorType] = getSelectorData(element, selectorType); | ||
}); | ||
return result; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {assert} from 'chai' | ||
import {createMemo} from '../src/memo' | ||
|
||
describe('memo', () => { | ||
|
||
let root | ||
|
||
/** | ||
* Simple way to retrieve target element for test. | ||
* @returns {Element} | ||
*/ | ||
function getTargetElement () { | ||
return root.querySelector('[data-target]') | ||
} | ||
|
||
beforeEach(() => { | ||
root = document.body.appendChild(document.createElement('div')) | ||
}) | ||
|
||
afterEach(() => { | ||
root.parentNode.removeChild(root) | ||
}) | ||
|
||
it('should retrieve selectors for an element', () => { | ||
root.innerHTML = '<div data-target></div>' | ||
const element = getTargetElement() | ||
const getElementSelectors = createMemo() | ||
const result = getElementSelectors(element, ['tag']) | ||
assert.deepEqual(result, {tag: ['div']}) | ||
}) | ||
|
||
it('should use remembered value', () => { | ||
root.innerHTML = '<div data-target></div>' | ||
const element = getTargetElement() | ||
const memoData = new Map([ | ||
[ | ||
element, | ||
new Map([ | ||
['tag', ['mock_tag_selector']] | ||
]) | ||
] | ||
]) | ||
const getElementSelectors = createMemo(memoData) | ||
const result = getElementSelectors(element, ['tag']) | ||
assert.deepEqual(result, {tag: ['mock_tag_selector']}) | ||
}) | ||
}) |