UNPKG

2.08 kBJavaScriptView Raw
1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3import { StorageHelper } from '@aws-amplify/core';
4/**
5 * Default cache config
6 */
7export var defaultConfig = {
8 keyPrefix: 'aws-amplify-cache',
9 capacityInBytes: 1048576,
10 itemMaxSize: 210000,
11 defaultTTL: 259200000,
12 defaultPriority: 5,
13 warningThreshold: 0.8,
14 // the storage helper will check if localStorage exists,
15 // if not, will use a in-memory object instead
16 storage: new StorageHelper().getStorage(),
17};
18/**
19 * return the byte size of the string
20 * @param str
21 */
22export function getByteLength(str) {
23 var ret = 0;
24 ret = str.length;
25 for (var i = str.length; i >= 0; i -= 1) {
26 var charCode = str.charCodeAt(i);
27 if (charCode > 0x7f && charCode <= 0x7ff) {
28 ret += 1;
29 }
30 else if (charCode > 0x7ff && charCode <= 0xffff) {
31 ret += 2;
32 }
33 // trail surrogate
34 if (charCode >= 0xdc00 && charCode <= 0xdfff) {
35 i -= 1;
36 }
37 }
38 return ret;
39}
40/**
41 * get current time
42 */
43export function getCurrTime() {
44 var currTime = new Date();
45 return currTime.getTime();
46}
47/**
48 * check if passed value is an integer
49 */
50export function isInteger(value) {
51 if (Number.isInteger) {
52 return Number.isInteger(value);
53 }
54 return _isInteger(value);
55}
56function _isInteger(value) {
57 return (typeof value === 'number' && isFinite(value) && Math.floor(value) === value);
58}
59/**
60 * provide an object as the in-memory cache
61 */
62var store = {};
63var CacheObject = /** @class */ (function () {
64 function CacheObject() {
65 }
66 CacheObject.clear = function () {
67 store = {};
68 };
69 CacheObject.getItem = function (key) {
70 return store[key] || null;
71 };
72 CacheObject.setItem = function (key, value) {
73 store[key] = value;
74 };
75 CacheObject.removeItem = function (key) {
76 delete store[key];
77 };
78 return CacheObject;
79}());
80export { CacheObject };
81//# sourceMappingURL=CacheUtils.js.map
\No newline at end of file