UNPKG

2.72 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
6 * the License. A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0/
9 *
10 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
12 * and limitations under the License.
13 */
14Object.defineProperty(exports, "__esModule", { value: true });
15var core_1 = require("@aws-amplify/core");
16/**
17 * Default cache config
18 */
19exports.defaultConfig = {
20 keyPrefix: 'aws-amplify-cache',
21 capacityInBytes: 1048576,
22 itemMaxSize: 210000,
23 defaultTTL: 259200000,
24 defaultPriority: 5,
25 warningThreshold: 0.8,
26 // the storage helper will check if localStorage exists,
27 // if not, will use a in-memory object instead
28 storage: new core_1.StorageHelper().getStorage(),
29};
30/**
31 * return the byte size of the string
32 * @param str
33 */
34function getByteLength(str) {
35 var ret = 0;
36 ret = str.length;
37 for (var i = str.length; i >= 0; i -= 1) {
38 var charCode = str.charCodeAt(i);
39 if (charCode > 0x7f && charCode <= 0x7ff) {
40 ret += 1;
41 }
42 else if (charCode > 0x7ff && charCode <= 0xffff) {
43 ret += 2;
44 }
45 // trail surrogate
46 if (charCode >= 0xdc00 && charCode <= 0xdfff) {
47 i -= 1;
48 }
49 }
50 return ret;
51}
52exports.getByteLength = getByteLength;
53/**
54 * get current time
55 */
56function getCurrTime() {
57 var currTime = new Date();
58 return currTime.getTime();
59}
60exports.getCurrTime = getCurrTime;
61/**
62 * check if passed value is an integer
63 */
64function isInteger(value) {
65 if (Number.isInteger) {
66 return Number.isInteger(value);
67 }
68 return _isInteger(value);
69}
70exports.isInteger = isInteger;
71function _isInteger(value) {
72 return (typeof value === 'number' && isFinite(value) && Math.floor(value) === value);
73}
74/**
75 * provide an object as the in-memory cache
76 */
77var store = {};
78var CacheObject = /** @class */ (function () {
79 function CacheObject() {
80 }
81 CacheObject.clear = function () {
82 store = {};
83 };
84 CacheObject.getItem = function (key) {
85 return store[key] || null;
86 };
87 CacheObject.setItem = function (key, value) {
88 store[key] = value;
89 };
90 CacheObject.removeItem = function (key) {
91 delete store[key];
92 };
93 return CacheObject;
94}());
95exports.CacheObject = CacheObject;
96//# sourceMappingURL=CacheUtils.js.map
\No newline at end of file