Skip to content

Commit

Permalink
moar stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Creamer committed Sep 25, 2020
1 parent c2b67c8 commit eeef969
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
29 changes: 29 additions & 0 deletions src/problems/element.ts
@@ -0,0 +1,29 @@
const findPath = (element) => {
const path = [];
let current = element;

while (current.parentNode) {
const index = current.parentNode.children.indexOf(current);
path.push(index);
current = current.parentNode;
}

return path;
}

const walkPath = (root, path) => {
let current = root;

while (path.length) {
current = current.children[path.pop()];
}

return current;
}

const findElement = (root, element) => {
const path = findPath(element);
const foundIt = walkPath(root, path);

return foundIt;
}
8 changes: 7 additions & 1 deletion src/problems/lru.test.ts
@@ -1,7 +1,7 @@
import assert from 'assert';
import { LRU } from './lru';

const cache = new LRU<string>();
const cache = new LRU<string>(5);

cache.set({ key: 'foo', value: 'bar' });
cache.set({ key: 'bar', value: 'foo' });
Expand All @@ -22,3 +22,9 @@ assert.equal(cache.items.head.value.key, 'foo');
cache.set({ key: 'abc123', value: 'my value' });
assert.equal(cache.items.head.value.key, 'abc123');
assert.equal(cache.size, 5);

cache.set({ key: 'abc456', value: 'my last value' });
assert.equal(cache.size, 5);
assert.equal(cache.items.head.value.key, 'abc456');
assert.equal(cache.cache.has('bar'), false);
assert.equal(cache.cache.get('abc456').value.value, 'my last value')
9 changes: 6 additions & 3 deletions src/problems/lru.ts
Expand Up @@ -12,13 +12,16 @@ export class LRU<ValueType> {
items: DoublyLinkedList<CacheItem<ValueType>>;
cache: Map<string, DoublyLinkedListNode<CacheItem<ValueType>>> = new Map();

constructor() {
constructor(limit) {
this.items = new DoublyLinkedList<CacheItem<ValueType>>();
this.limit = limit;
}

set(item: CacheItem<ValueType>) {
if (this.items.count > this.limit) {
this.items.deleteTail();
if (this.items.count + 1 > this.limit) {
const tail = this.items.deleteTail();
this.size -= 1;
this.cache.delete(tail.value.key)
}

this.items.prepend(item);
Expand Down
32 changes: 32 additions & 0 deletions src/problems/moveElement.ts
@@ -0,0 +1,32 @@
const paint = (start, elapsed, duration, animate) => {
if (elapsed < duration) {
animate(elapsed);
requestAnimationFrame((step) => paint(start, step - start, duration, animate));
}
}

const translate = (el, distance = 0, direction = 'X') => {
el.dataset.translate = el.dataset.translate || JSON.stringify({
X: distance,
Y: distance,
});
const saved = JSON.parse(el.dataset.translate);
saved[direction] = distance;
el.dataset.translate = JSON.stringify(saved);

el.style.transform = `translate${direction}(${saved[direction]}px)`;
};

const moveElement = (duration, distance, direction, element) => {
const onFrame = (
elapsed
) => {
const progress = elapsed / duration;
const amount = progress * distance;

translate(element, Math.min(amount, duration), direction);
};

requestAnimationFrame((step) => paint(step, 0, duration, onFrame));
}
moveElement(10000, 750, 'X', button);

0 comments on commit eeef969

Please sign in to comment.