File tree 2 files changed +28
-0
lines changed
2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Copyright 2012-2017, Plotly, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ 'use strict' ;
10
+
11
+ /*
12
+ * Ensures an array has the right amount of storage space. If it doesn't
13
+ * exist, it creates an array. If it does exist, it returns it if too
14
+ * short or truncates it in-place.
15
+ *
16
+ * The goal is to just reuse memory to avoid a bit of excessive garbage
17
+ * collection.
18
+ */
19
+ module . exports = function ensureArray ( out , n ) {
20
+ if ( ! Array . isArray ( out ) ) out = [ ] ;
21
+
22
+ // If too long, truncate. (If too short, it will grow
23
+ // automatically so we don't care about that case)
24
+ out . length = n ;
25
+
26
+ return out ;
27
+ } ;
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ lib.isArray = require('./is_array');
19
19
lib . mod = require ( './mod' ) ;
20
20
lib . toLogRange = require ( './to_log_range' ) ;
21
21
lib . relinkPrivateKeys = require ( './relink_private' ) ;
22
+ lib . ensureArray = require ( './ensure_array' ) ;
22
23
23
24
var coerceModule = require ( './coerce' ) ;
24
25
lib . valObjects = coerceModule . valObjects ;
You can’t perform that action at this time.
0 commit comments