comparison src/implab/safe.js @ 3:00779cb63b12

formatting
author cin
date Tue, 06 Jun 2017 19:45:32 +0300
parents fc2517695ee1
children 9c0943c68a90
comparison
equal deleted inserted replaced
2:7d7059d2a810 3:00779cb63b12
1 define([], 1 define([],
2 2
3 function () { 3 function () {
4 var id = 0, 4 var _create = Object.create,
5 OID_FIELD = "__core_safe_oid_field",
6 _create, _defineProperty, _keys;
7
8 if (!Object.keys) {
9 _defineProperty = function (obj, prop, d) {
10 if (!d)
11 throw new Error("Invalid descriptor");
12 obj[prop] = d.value;
13 };
14 } else {
15 _defineProperty = Object.defineProperty;
16 }
17
18 if (!Object.create) {
19 var tctor = function () {};
20 _create = function (proto) {
21 if (arguments.length > 1)
22 throw new Error("The two arguments for isn't supported");
23
24 tctor.prototype = proto;
25 return new tctor();
26 };
27 } else {
28 _create = Object.create;
29 }
30
31 if (!Object.keys) {
32 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Polyfill
33 _keys = (function () {
34 'use strict';
35 var hasOwnProperty = Object.prototype.hasOwnProperty,
36 hasDontEnumBug = !({
37 toString: null
38 }).propertyIsEnumerable('toString'),
39 dontEnums = [
40 'toString',
41 'toLocaleString',
42 'valueOf',
43 'hasOwnProperty',
44 'isPrototypeOf',
45 'propertyIsEnumerable',
46 'constructor'
47 ],
48 dontEnumsLength = dontEnums.length;
49
50 return function (obj) {
51 if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
52 throw new TypeError('Object.keys called on non-object');
53 }
54
55 var result = [],
56 prop, i;
57
58 for (prop in obj) {
59 if (prop != OID_FIELD && hasOwnProperty.call(obj, prop)) {
60 result.push(prop);
61 }
62 }
63
64 if (hasDontEnumBug) {
65 for (i = 0; i < dontEnumsLength; i++) {
66 if (hasOwnProperty.call(obj, dontEnums[i])) {
67 result.push(dontEnums[i]);
68 }
69 }
70 }
71 return result;
72 };
73 }());
74 } else {
75 _keys = Object.keys; 5 _keys = Object.keys;
76 }
77 6
78 var safe = null; 7 var safe = null;
79 safe = { 8 safe = {
80 OID_FIELD: OID_FIELD,
81
82 argumentNotNull: function (arg, name) { 9 argumentNotNull: function (arg, name) {
83 if (arg === null || arg === undefined) 10 if (arg === null || arg === undefined)
84 throw new Error("The argument " + name + " can't be null or undefined"); 11 throw new Error("The argument " + name + " can't be null or undefined");
85 }, 12 },
86 13
158 x = cb.call(thisArg, obj[k], k); 85 x = cb.call(thisArg, obj[k], k);
159 if (x !== undefined) 86 if (x !== undefined)
160 return x; 87 return x;
161 } 88 }
162 } 89 }
163 },
164
165 oid: function (obj) {
166 return this.isPrimitive(obj) ? undefined : obj[OID_FIELD] ||
167 (_defineProperty(obj, OID_FIELD, {
168 value: ++id,
169 enumerable: false
170 }) && id);
171 }, 90 },
172 91
173 /** 92 /**
174 * Копирует свойства одного объекта в другой. 93 * Копирует свойства одного объекта в другой.
175 * 94 *
217 } 136 }
218 } 137 }
219 return dest; 138 return dest;
220 }, 139 },
221 140
222 create: _create, 141 create: function () {
142 if (console && console.warn)
143 console.warn("implab/safe::create is deprecated use Object.create instead");
144 _create.apply(this, arguments);
145 },
223 146
224 delegate: function (target, method) { 147 delegate: function (target, method) {
225 if (!(method instanceof Function)) { 148 if (!(method instanceof Function)) {
226 this.argumentNotNull(target, "target"); 149 this.argumentNotNull(target, "target");
227 method = target[method]; 150 method = target[method];
342 throw new Error("The sequence is empty"); 265 throw new Error("The sequence is empty");
343 } 266 }
344 return cb ? cb(sequence[0]) : sequence[0]; 267 return cb ? cb(sequence[0]) : sequence[0];
345 } 268 }
346 } 269 }
347 270
348 if (err) 271 if (err)
349 return err(new Error("The sequence is required")); 272 return err(new Error("The sequence is required"));
350 else 273 else
351 throw new Error("The sequence is required"); 274 throw new Error("The sequence is required");
352 } 275 }