Node-Red configuration
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

index.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. 'use strict';
  2. var test = require('tape');
  3. var v = require('es-value-fixtures');
  4. var forEach = require('for-each');
  5. var inspect = require('object-inspect');
  6. var hasOwn = require('hasown');
  7. var hasPropertyDescriptors = require('has-property-descriptors')();
  8. var getOwnPropertyDescriptors = require('object.getownpropertydescriptors');
  9. var ownKeys = require('reflect.ownkeys');
  10. var defineDataProperty = require('../');
  11. test('defineDataProperty', function (t) {
  12. t.test('argument validation', function (st) {
  13. forEach(v.primitives, function (nonObject) {
  14. st['throws'](
  15. // @ts-expect-error
  16. function () { defineDataProperty(nonObject, 'key', 'value'); },
  17. TypeError,
  18. 'throws on non-object input: ' + inspect(nonObject)
  19. );
  20. });
  21. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  22. st['throws'](
  23. // @ts-expect-error
  24. function () { defineDataProperty({}, nonPropertyKey, 'value'); },
  25. TypeError,
  26. 'throws on non-PropertyKey input: ' + inspect(nonPropertyKey)
  27. );
  28. });
  29. forEach(v.nonBooleans, function (nonBoolean) {
  30. if (nonBoolean !== null) {
  31. st['throws'](
  32. // @ts-expect-error
  33. function () { defineDataProperty({}, 'key', 'value', nonBoolean); },
  34. TypeError,
  35. 'throws on non-boolean nonEnumerable: ' + inspect(nonBoolean)
  36. );
  37. st['throws'](
  38. // @ts-expect-error
  39. function () { defineDataProperty({}, 'key', 'value', false, nonBoolean); },
  40. TypeError,
  41. 'throws on non-boolean nonWritable: ' + inspect(nonBoolean)
  42. );
  43. st['throws'](
  44. // @ts-expect-error
  45. function () { defineDataProperty({}, 'key', 'value', false, false, nonBoolean); },
  46. TypeError,
  47. 'throws on non-boolean nonConfigurable: ' + inspect(nonBoolean)
  48. );
  49. }
  50. });
  51. st.end();
  52. });
  53. t.test('normal data property', function (st) {
  54. /** @type {Record<PropertyKey, string>} */
  55. var obj = { existing: 'existing property' };
  56. st.ok(hasOwn(obj, 'existing'), 'has initial own property');
  57. st.equal(obj.existing, 'existing property', 'has expected initial value');
  58. var res = defineDataProperty(obj, 'added', 'added property');
  59. st.equal(res, void undefined, 'returns `undefined`');
  60. st.ok(hasOwn(obj, 'added'), 'has expected own property');
  61. st.equal(obj.added, 'added property', 'has expected value');
  62. defineDataProperty(obj, 'existing', 'new value');
  63. st.ok(hasOwn(obj, 'existing'), 'still has expected own property');
  64. st.equal(obj.existing, 'new value', 'has new expected value');
  65. defineDataProperty(obj, 'explicit1', 'new value', false);
  66. st.ok(hasOwn(obj, 'explicit1'), 'has expected own property (explicit enumerable)');
  67. st.equal(obj.explicit1, 'new value', 'has new expected value (explicit enumerable)');
  68. defineDataProperty(obj, 'explicit2', 'new value', false, false);
  69. st.ok(hasOwn(obj, 'explicit2'), 'has expected own property (explicit writable)');
  70. st.equal(obj.explicit2, 'new value', 'has new expected value (explicit writable)');
  71. defineDataProperty(obj, 'explicit3', 'new value', false, false, false);
  72. st.ok(hasOwn(obj, 'explicit3'), 'has expected own property (explicit configurable)');
  73. st.equal(obj.explicit3, 'new value', 'has new expected value (explicit configurable)');
  74. st.end();
  75. });
  76. t.test('loose mode', { skip: !hasPropertyDescriptors }, function (st) {
  77. var obj = { existing: 'existing property' };
  78. defineDataProperty(obj, 'added', 'added value 1', true, null, null, true);
  79. st.deepEqual(
  80. getOwnPropertyDescriptors(obj),
  81. {
  82. existing: {
  83. configurable: true,
  84. enumerable: true,
  85. value: 'existing property',
  86. writable: true
  87. },
  88. added: {
  89. configurable: true,
  90. enumerable: !hasPropertyDescriptors,
  91. value: 'added value 1',
  92. writable: true
  93. }
  94. },
  95. 'in loose mode, obj still adds property 1'
  96. );
  97. defineDataProperty(obj, 'added', 'added value 2', false, true, null, true);
  98. st.deepEqual(
  99. getOwnPropertyDescriptors(obj),
  100. {
  101. existing: {
  102. configurable: true,
  103. enumerable: true,
  104. value: 'existing property',
  105. writable: true
  106. },
  107. added: {
  108. configurable: true,
  109. enumerable: true,
  110. value: 'added value 2',
  111. writable: !hasPropertyDescriptors
  112. }
  113. },
  114. 'in loose mode, obj still adds property 2'
  115. );
  116. defineDataProperty(obj, 'added', 'added value 3', false, false, true, true);
  117. st.deepEqual(
  118. getOwnPropertyDescriptors(obj),
  119. {
  120. existing: {
  121. configurable: true,
  122. enumerable: true,
  123. value: 'existing property',
  124. writable: true
  125. },
  126. added: {
  127. configurable: !hasPropertyDescriptors,
  128. enumerable: true,
  129. value: 'added value 3',
  130. writable: true
  131. }
  132. },
  133. 'in loose mode, obj still adds property 3'
  134. );
  135. st.end();
  136. });
  137. t.test('non-normal data property, ES3', { skip: hasPropertyDescriptors }, function (st) {
  138. /** @type {Record<PropertyKey, string>} */
  139. var obj = { existing: 'existing property' };
  140. st['throws'](
  141. function () { defineDataProperty(obj, 'added', 'added value', true); },
  142. SyntaxError,
  143. 'nonEnumerable throws a Syntax Error'
  144. );
  145. st['throws'](
  146. function () { defineDataProperty(obj, 'added', 'added value', false, true); },
  147. SyntaxError,
  148. 'nonWritable throws a Syntax Error'
  149. );
  150. st['throws'](
  151. function () { defineDataProperty(obj, 'added', 'added value', false, false, true); },
  152. SyntaxError,
  153. 'nonWritable throws a Syntax Error'
  154. );
  155. st.deepEqual(
  156. ownKeys(obj),
  157. ['existing'],
  158. 'obj still has expected keys'
  159. );
  160. st.equal(obj.existing, 'existing property', 'obj still has expected values');
  161. st.end();
  162. });
  163. t.test('new non-normal data property, ES5+', { skip: !hasPropertyDescriptors }, function (st) {
  164. /** @type {Record<PropertyKey, string>} */
  165. var obj = { existing: 'existing property' };
  166. defineDataProperty(obj, 'nonEnum', null, true);
  167. defineDataProperty(obj, 'nonWrit', null, false, true);
  168. defineDataProperty(obj, 'nonConf', null, false, false, true);
  169. st.deepEqual(
  170. getOwnPropertyDescriptors(obj),
  171. {
  172. existing: {
  173. configurable: true,
  174. enumerable: true,
  175. value: 'existing property',
  176. writable: true
  177. },
  178. nonEnum: {
  179. configurable: true,
  180. enumerable: false,
  181. value: null,
  182. writable: true
  183. },
  184. nonWrit: {
  185. configurable: true,
  186. enumerable: true,
  187. value: null,
  188. writable: false
  189. },
  190. nonConf: {
  191. configurable: false,
  192. enumerable: true,
  193. value: null,
  194. writable: true
  195. }
  196. },
  197. 'obj has expected property descriptors'
  198. );
  199. st.end();
  200. });
  201. t.test('existing non-normal data property, ES5+', { skip: !hasPropertyDescriptors }, function (st) {
  202. // test case changing an existing non-normal property
  203. /** @type {Record<string, null | string>} */
  204. var obj = {};
  205. Object.defineProperty(obj, 'nonEnum', { configurable: true, enumerable: false, value: null, writable: true });
  206. Object.defineProperty(obj, 'nonWrit', { configurable: true, enumerable: true, value: null, writable: false });
  207. Object.defineProperty(obj, 'nonConf', { configurable: false, enumerable: true, value: null, writable: true });
  208. st.deepEqual(
  209. getOwnPropertyDescriptors(obj),
  210. {
  211. nonEnum: {
  212. configurable: true,
  213. enumerable: false,
  214. value: null,
  215. writable: true
  216. },
  217. nonWrit: {
  218. configurable: true,
  219. enumerable: true,
  220. value: null,
  221. writable: false
  222. },
  223. nonConf: {
  224. configurable: false,
  225. enumerable: true,
  226. value: null,
  227. writable: true
  228. }
  229. },
  230. 'obj initially has expected property descriptors'
  231. );
  232. defineDataProperty(obj, 'nonEnum', 'new value', false);
  233. defineDataProperty(obj, 'nonWrit', 'new value', false, false);
  234. st['throws'](
  235. function () { defineDataProperty(obj, 'nonConf', 'new value', false, false, false); },
  236. TypeError,
  237. 'can not alter a nonconfigurable property'
  238. );
  239. st.deepEqual(
  240. getOwnPropertyDescriptors(obj),
  241. {
  242. nonEnum: {
  243. configurable: true,
  244. enumerable: true,
  245. value: 'new value',
  246. writable: true
  247. },
  248. nonWrit: {
  249. configurable: true,
  250. enumerable: true,
  251. value: 'new value',
  252. writable: true
  253. },
  254. nonConf: {
  255. configurable: false,
  256. enumerable: true,
  257. value: null,
  258. writable: true
  259. }
  260. },
  261. 'obj ends up with expected property descriptors'
  262. );
  263. st.end();
  264. });
  265. t.test('frozen object, ES5+', { skip: !hasPropertyDescriptors }, function (st) {
  266. var frozen = Object.freeze({ existing: true });
  267. st['throws'](
  268. function () { defineDataProperty(frozen, 'existing', 'new value'); },
  269. TypeError,
  270. 'frozen object can not modify an existing property'
  271. );
  272. st['throws'](
  273. function () { defineDataProperty(frozen, 'new', 'new property'); },
  274. TypeError,
  275. 'frozen object can not add a new property'
  276. );
  277. st.end();
  278. });
  279. t.test('sealed object, ES5+', { skip: !hasPropertyDescriptors }, function (st) {
  280. var sealed = Object.seal({ existing: true });
  281. st.deepEqual(
  282. Object.getOwnPropertyDescriptor(sealed, 'existing'),
  283. {
  284. configurable: false,
  285. enumerable: true,
  286. value: true,
  287. writable: true
  288. },
  289. 'existing value on sealed object has expected descriptor'
  290. );
  291. defineDataProperty(sealed, 'existing', 'new value');
  292. st.deepEqual(
  293. Object.getOwnPropertyDescriptor(sealed, 'existing'),
  294. {
  295. configurable: false,
  296. enumerable: true,
  297. value: 'new value',
  298. writable: true
  299. },
  300. 'existing value on sealed object has changed descriptor'
  301. );
  302. st['throws'](
  303. function () { defineDataProperty(sealed, 'new', 'new property'); },
  304. TypeError,
  305. 'sealed object can not add a new property'
  306. );
  307. st.end();
  308. });
  309. t.test('nonextensible object, ES5+', { skip: !hasPropertyDescriptors }, function (st) {
  310. var nonExt = Object.preventExtensions({ existing: true });
  311. st.deepEqual(
  312. Object.getOwnPropertyDescriptor(nonExt, 'existing'),
  313. {
  314. configurable: true,
  315. enumerable: true,
  316. value: true,
  317. writable: true
  318. },
  319. 'existing value on non-extensible object has expected descriptor'
  320. );
  321. defineDataProperty(nonExt, 'existing', 'new value', true);
  322. st.deepEqual(
  323. Object.getOwnPropertyDescriptor(nonExt, 'existing'),
  324. {
  325. configurable: true,
  326. enumerable: false,
  327. value: 'new value',
  328. writable: true
  329. },
  330. 'existing value on non-extensible object has changed descriptor'
  331. );
  332. st['throws'](
  333. function () { defineDataProperty(nonExt, 'new', 'new property'); },
  334. TypeError,
  335. 'non-extensible object can not add a new property'
  336. );
  337. st.end();
  338. });
  339. t.end();
  340. });