1 | "use strict";
|
2 | var __assign = (this && this.__assign) || Object.assign || function(t) {
|
3 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4 | s = arguments[i];
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6 | t[p] = s[p];
|
7 | }
|
8 | return t;
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | var CacheNode_1 = require("./CacheNode");
|
12 | var cache_1 = require("./cache");
|
13 | var locate_1 = require("./locate");
|
14 | var toString = Object.prototype.toString;
|
15 | var _hasOwnProperty = Object.prototype.hasOwnProperty;
|
16 | function isNumber(value) {
|
17 | return typeof value === 'number' || toString(value) === '[object Number]';
|
18 | }
|
19 | exports.isNumber = isNumber;
|
20 | function isString(obj) {
|
21 | return typeof obj === 'string' || toString(obj) === '[object String]';
|
22 | }
|
23 | exports.isString = isString;
|
24 | function isObject(mixedVar) {
|
25 | if (Object.prototype.toString.call(mixedVar) === '[object Array]') {
|
26 | return false;
|
27 | }
|
28 | return mixedVar !== null && typeof mixedVar === 'object';
|
29 | }
|
30 | exports.isObject = isObject;
|
31 | function isFunction(item) {
|
32 | return typeof item === 'function';
|
33 | }
|
34 | exports.isFunction = isFunction;
|
35 | function isArray(value) {
|
36 | if (!value || value === null) {
|
37 | return false;
|
38 | }
|
39 | return Array.isArray(value) || (value && typeof value === 'object'
|
40 | && typeof value.length === 'number'
|
41 | && typeof value.splice === 'function'
|
42 | && !(value.propertyIsEnumerable('length')));
|
43 | }
|
44 | exports.isArray = isArray;
|
45 | function objToStr(o) {
|
46 | return Object.prototype.toString.call(o);
|
47 | }
|
48 | function isDate(value) {
|
49 | return isObject(value) && objToStr(value) === '[object Date]';
|
50 | }
|
51 | exports.isDate = isDate;
|
52 | function isEmpty(value) {
|
53 | if (!value) {
|
54 | return true;
|
55 | }
|
56 | if (isArray(value) && value.length === 0) {
|
57 | return true;
|
58 | }
|
59 | else if (!isString(value)) {
|
60 | for (var i in value) {
|
61 | if (_hasOwnProperty.call(value, i)) {
|
62 | return false;
|
63 | }
|
64 | }
|
65 | return true;
|
66 | }
|
67 | return false;
|
68 | }
|
69 | exports.isEmpty = isEmpty;
|
70 | function getNewCacheNode(instance) {
|
71 | var node = new CacheNode_1.CacheNode(instance.nextNodeKey);
|
72 | node.id = instance.nextNodeKey;
|
73 | instance.nextNodeKey += 1;
|
74 | instance.repo.add(node);
|
75 | return node;
|
76 | }
|
77 | exports.getNewCacheNode = getNewCacheNode;
|
78 | function hasUid(obj) {
|
79 | if (!obj) {
|
80 | return false;
|
81 | }
|
82 | if (!isObject(obj)) {
|
83 | return false;
|
84 | }
|
85 | if (typeof obj[cache_1.config.uidName] === 'undefined') {
|
86 | return false;
|
87 | }
|
88 | var uid = obj[cache_1.config.uidName];
|
89 | return uid.length !== 0;
|
90 | }
|
91 | exports.hasUid = hasUid;
|
92 | Function.prototype.clone = function (target) {
|
93 | var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
|
94 | var ARGUMENT_NAMES = /([^\s,]+)/g;
|
95 | function getParamNames(func) {
|
96 | var fnStr = func.toString().replace(STRIP_COMMENTS, '');
|
97 | var result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
|
98 | if (result === null) {
|
99 | result = [];
|
100 | }
|
101 | return result;
|
102 | }
|
103 | var stringify = this.toString();
|
104 | stringify = stringify.replace(new RegExp('_this', 'g'), 'this');
|
105 | var body = stringify.match(/function[^{]+\{([\s\S]*)\}$/)[1];
|
106 | body = body.trim();
|
107 | var paramNames = getParamNames(this);
|
108 | var func;
|
109 | if (body.indexOf('native code') < 0) {
|
110 | func = Function(paramNames, body);
|
111 | func = func.bind(target);
|
112 | }
|
113 | return func;
|
114 | };
|
115 | function deepClone(obj, uidReference, freeze) {
|
116 | if (freeze === void 0) { freeze = true; }
|
117 | if (!obj
|
118 | || (!isObject(obj)
|
119 | && !isArray(obj))) {
|
120 | return obj;
|
121 | }
|
122 | if (freeze === true
|
123 | && uidReference
|
124 | && !Object.isFrozen(uidReference)) {
|
125 | Object.freeze(uidReference);
|
126 | }
|
127 | if (uidReference
|
128 | && hasUid(obj)
|
129 | && obj[cache_1.config.uidName] === uidReference[cache_1.config.uidName]) {
|
130 | return uidReference;
|
131 | }
|
132 | var result = __assign({}, obj);
|
133 | for (var propName in obj) {
|
134 | var value = obj[propName];
|
135 | if (value) {
|
136 | if (isArray(value)) {
|
137 | result[propName] = deepCloneArray(value, uidReference, freeze);
|
138 | }
|
139 | else if (isDate(value)) {
|
140 | var date = new Date(value.getTime());
|
141 | if (freeze === true) {
|
142 | Object.freeze(date);
|
143 | }
|
144 | result[propName] = date;
|
145 | }
|
146 | else if (isObject(value)) {
|
147 | if (hasUid(value)) {
|
148 | result[propName] = value;
|
149 | if (uidReference && hasUid(uidReference)) {
|
150 | if (value !== uidReference
|
151 | && value.uid === uidReference.uid
|
152 | && value !== uidReference) {
|
153 | result[propName] = uidReference;
|
154 | }
|
155 | }
|
156 | else {
|
157 | }
|
158 | }
|
159 | else {
|
160 | result[propName] = deepClone(value, uidReference, freeze);
|
161 | }
|
162 | }
|
163 | else if (isFunction(value)) {
|
164 | if (propName !== 'constructor') {
|
165 | result[propName] = value.clone(result);
|
166 | }
|
167 | }
|
168 | else {
|
169 | result[propName] = value;
|
170 | }
|
171 | }
|
172 | }
|
173 | if (freeze === true
|
174 | && !Object.isFrozen(result)
|
175 | && typeof result !== 'function') {
|
176 | Object.freeze(result);
|
177 | }
|
178 | return result;
|
179 | }
|
180 | exports.deepClone = deepClone;
|
181 | function deepCloneArray(arr, uidReference, freeze) {
|
182 | return arr.map(function (item) {
|
183 | if (isArray(item)) {
|
184 | return deepCloneArray(item, uidReference, freeze);
|
185 | }
|
186 | else if (isObject(item)) {
|
187 | if (hasUid(item)) {
|
188 | if (uidReference && (item[cache_1.config.uidName] === uidReference[cache_1.config.uidName])) {
|
189 | return uidReference;
|
190 | }
|
191 | return item;
|
192 | }
|
193 | else {
|
194 | return deepClone(item, uidReference, freeze);
|
195 | }
|
196 | }
|
197 | else {
|
198 | return item;
|
199 | }
|
200 | });
|
201 | }
|
202 | exports.cacheSize = function (instance) {
|
203 | var cacheNode = locate_1.getCurrentNode(instance);
|
204 | return cacheNode ? cacheNode.items.size() : 0;
|
205 | };
|
206 | exports.cacheLength = function (instance) { return (instance.thread.nodes.length); };
|