當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript og-lru-cache-factory.OgLruCache類代碼示例

本文整理匯總了TypeScript中og-components/og-lru-cache-factory/models/og-lru-cache-factory.OgLruCache的典型用法代碼示例。如果您正苦於以下問題:TypeScript OgLruCache類的具體用法?TypeScript OgLruCache怎麽用?TypeScript OgLruCache使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了OgLruCache類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: describe

	describe("new", (): void => {
		let ogLruCache: OgLruCache;

		it("should return an LruCache", (): Chai.Assertion => ogLruCacheFactory.new(10, []).should.be.an("object"));

		describe("LruCache (empty)", (): void => {
			beforeEach((): OgLruCache => (ogLruCache = ogLruCacheFactory.new(10, [])));

			it("should have the specified capacity", (): Chai.Assertion => ogLruCache["capacity"].should.equal(10));

			it("should have no items", (): Chai.Assertion => ogLruCache.list.should.be.an("array").that.is.empty);
		});

		describe("LruCache", (): void => {
			const capacity: number = 10;
			let	data: OgCacheEntry[],
					list: OgCacheEntry[];

			beforeEach((): void => {
				// Populate the data with capacity + 1 items (to check that the oldest item is evicted)
				data = [...Array(capacity + 1).keys()].reverse().map((id: number): OgCacheEntry => ({id, name: `item ${id}`}));
				list = data.slice(0, capacity);

				// Create the LruCache
				ogLruCache = ogLruCacheFactory.new(capacity, data);
			});

			it("should be populated with the initial set of items", (): Chai.Assertion => ogLruCache["items"].should.deep.equal(list));

			describe("put", (): void => {
				it("should leave the list unchanged if the item is already the current head", (): Chai.Assertion => ogLruCache.put({id: capacity, name: `item ${capacity}`}).should.deep.equal(list));

				const scenarios: {description: string, item: OgCacheEntry, currentIndex?: number}[] = [
					{
						description: "move an existing item from the tail of the list to the head of the list",
						item: {id: 1, name: "item 1"}
					},
					{
						description: "move an existing item to the head of the list",
						item: {id: 2, name: "item 2"},
						currentIndex: 8
					},
					{
						description: "add a new item to the list",
						item: {id: 11, name: "item 11"}
					}
				];

				scenarios.forEach((scenario: {description: string, item: OgCacheEntry, currentIndex?: number}): void => {
					it(`should ${scenario.description}`, (): void => {
						const newList: OgCacheEntry[] = ogLruCache.put(scenario.item);

						if (scenario.currentIndex) {
							list.splice(scenario.currentIndex, 1);
						} else {
							list.pop();
						}
						list.unshift(scenario.item);
						newList.should.deep.equal(list);
					});
				});

				it("should add a new item to an empty list", (): void => {
					ogLruCache["items"] = [];

					const item: OgCacheEntry = {id: 11, name: "item 11"},
								newList: OgCacheEntry[] = ogLruCache.put(item);

					list = [item];
					newList.should.deep.equal(list);
				});
			});

			describe("remove", (): void => {
				it("should leave the list unchanged if the item does not exist", (): Chai.Assertion => ogLruCache.remove(-1).should.deep.equal(list));

				describe("existing item", (): void => {
					let id: number;

					it("should remove the only item from the list", (): void => {
						id = 1;
						ogLruCache["items"] = [
							{id, name: `item ${id}`}
						];

						ogLruCache.remove(id);
						ogLruCache["items"].should.be.an("array").that.is.empty;
					});

					it("should remove an item from the head of the list", (): void => {
						id = 10;
						ogLruCache.remove(id);
						ogLruCache["items"].should.deep.equal(list.slice(1, capacity));
					});

					it("should remove an item from the tail of the list", (): void => {
						id = 1;
						ogLruCache.remove(id);
						ogLruCache["items"].should.deep.equal(list.slice(0, capacity - 1));
					});
//.........這裏部分代碼省略.........
開發者ID:scottohara,項目名稱:loot,代碼行數:101,代碼來源:og-lru-cache-factory.ts

示例2: removeRecent

	// Remove an item from the LRU cache
	public removeRecent(id: number): void {
		// Remove the item from the LRU cache
		this.recent = this.lruCache.remove(id);

		// Update local storage with the new list
		this.$window.localStorage.setItem(this.LRU_LOCAL_STORAGE_KEY, JSON.stringify(this.lruCache.list));
	}
開發者ID:scottohara,項目名稱:loot,代碼行數:8,代碼來源:payee.ts

示例3: addRecent

	// Put an item into the LRU cache
	public addRecent(payee: Payee): void {
		// Put the item into the LRU cache
		this.recent = this.lruCache.put(payee);

		// Update local storage with the new list
		this.$window.localStorage.setItem(this.LRU_LOCAL_STORAGE_KEY, JSON.stringify(this.lruCache.list));
	}
開發者ID:scottohara,項目名稱:loot,代碼行數:8,代碼來源:payee.ts

示例4:

					it("should remove the only item from the list", (): void => {
						id = 1;
						ogLruCache["items"] = [
							{id, name: `item ${id}`}
						];

						ogLruCache.remove(id);
						ogLruCache["items"].should.be.an("array").that.is.empty;
					});
開發者ID:scottohara,項目名稱:loot,代碼行數:9,代碼來源:og-lru-cache-factory.ts


注:本文中的og-components/og-lru-cache-factory/models/og-lru-cache-factory.OgLruCache類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。