incremental-dom
Version:
An in-place virtual DOM library
71 lines (62 loc) • 2.07 kB
text/typescript
/**
* Copyright 2018 The Incremental DOM Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* A cached reference to the hasOwnProperty function.
*/
const hasOwnProperty = Object.prototype.hasOwnProperty;
/**
* A constructor function that will create blank objects.
*/
function Blank() {}
Blank.prototype = Object.create(null);
/**
* Used to prevent property collisions between our "map" and its prototype.
* @param map The map to check.
* @param property The property to check.
* @return Whether map has property.
*/
function has(map: object, property: string): boolean {
return hasOwnProperty.call(map, property);
}
/**
* Creates an map object without a prototype.
* @returns An Object that can be used as a map.
*/
function createMap(): any {
return new (Blank as any)();
}
/**
* Truncates an array, removing items up until length.
* @param arr The array to truncate.
* @param length The new length of the array.
*/
function truncateArray(arr: Array<{} | null | undefined>, length: number) {
while (arr.length > length) {
arr.pop();
}
}
/**
* Creates an array for a desired initial size. Note that the array will still
* be empty.
* @param initialAllocationSize The initial size to allocate.
* @returns An empty array, with an initial allocation for the desired size.
*/
function createArray<T>(initialAllocationSize: number): Array<T> {
const arr = new Array(initialAllocationSize);
truncateArray(arr, 0);
return arr;
}
export { createArray, createMap, has, truncateArray };