Type range in javascript

Range

The Range interface represents a fragment of a document that can contain nodes and parts of text nodes.

A range can be created by using the Document.createRange() method. Range objects can also be retrieved by using the getRangeAt() method of the Selection object or the caretRangeFromPoint() method of the Document object.

There also is the Range() constructor available.

Instance properties

There are no inherited properties. Range.collapsed Read only Returns a boolean value indicating whether the range’s start and end points are at the same position. Range.commonAncestorContainer Read only Returns the deepest Node that contains the startContainer and endContainer nodes. Range.endContainer Read only Returns the Node within which the Range ends. Range.endOffset Read only Returns a number representing where in the endContainer the Range ends. Range.startContainer Read only Returns the Node within which the Range starts. Range.startOffset Read only Returns a number representing where in the startContainer the Range starts.

Constructor

Instance methods

There are no inherited methods. Range.collapse() Collapses the Range to one of its boundary points. Range.compareBoundaryPoints() Compares the boundary points of the Range with another Range . Range.compareNode() Deprecated Non-standard Returns a constant representing whether the Node is before, after, inside, or surrounding the range. Range.comparePoint() Returns -1, 0, or 1 indicating whether the point occurs before, inside, or after the Range . Range.cloneContents() Returns a DocumentFragment copying the nodes of a Range . Range.cloneRange() Returns a Range object with boundary points identical to the cloned Range . Range.createContextualFragment() Returns a DocumentFragment created from a given string of code. Range.deleteContents() Removes the contents of a Range from the Document . Range.detach() Does nothing. Kept for compatibility. Range.extractContents() Moves contents of a Range from the document tree into a DocumentFragment . Range.getBoundingClientRect() Returns a DOMRect object which bounds the entire contents of the Range ; this would be the union of all the rectangles returned by range.getClientRects() . Range.getClientRects() Returns a list of DOMRect objects that aggregates the results of Element.getClientRects() for all the elements in the Range . Range.isPointInRange() Returns a boolean indicating whether the given point is in the Range . Range.insertNode() Insert a Node at the start of a Range . Range.intersectsNode() Returns a boolean indicating whether the given node intersects the Range . Range.selectNode() Sets the Range to contain the Node and its contents. Range.selectNodeContents() Sets the Range to contain the contents of a Node . Range.setEnd() Sets the end position of a Range . Range.setStart() Sets the start position of a Range . Range.setEndAfter() Sets the end position of a Range relative to another Node . Range.setEndBefore() Sets the end position of a Range relative to another Node . Range.setStartAfter() Sets the start position of a Range relative to another Node . Range.setStartBefore() Sets the start position of a Range relative to another Node . Range.surroundContents() Moves content of a Range into a new Node . Range.toString() Returns the text of the Range .

Читайте также:  Change frame rate opencv python

Specifications

Browser compatibility

See also

Found a content problem with this page?

Источник

JavaScript Range How to create range in Javascript

Now take the leap of faith, assume that range(start, end) will just work. Then how do we solve the problem range(start, end) ? Simple! Just do [start, . range(start + 1, end)] . So combining both, we get

function range(start, end)  if(start === end) return [start]; return [start, . range(start + 1, end)]; > 

A lot more elegant than the for-loop solution in my opinion. But we could even go further if we use new Array(n) which creates an array with n elements. If we have an n element list, we could build a range from it by mapping each element to its index, i.e. arr.map((_, i) => i) . However, according to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#Description, map will not call for unassigned element. This mean we need to initialise the new Array(n) before mapping. One standard technique is to use fill . The final result is the following.

function range(start, end)  return (new Array(end - start + 1)).fill(undefined).map((_, i) => i + start); > 
function range(start, end)  return Array.from( length: end - start + 1 >, (_, i) => i) > 

Thank you Step for mentioning about efficiency when handling large ranges, which essentially build a huge array. We could have a more efficient way of doing this by using generators.

function* range(start, end)  for (let i = start; i  end; i++)  yield i; > > 

We could use this generator in a for. of loop (which would be very efficient) or use an array spread to retrieve all values (note that this essentially builds the array which is essentially the same as the non-generator approaches.)

for (i of range(1, 5))  console.log(i); > /* Output * 1 2 3 4 5 */ [. range(1, 5)] // [1, 2, 3, 4, 5] 
function* range(start, end)  yield start; if (start === end) return; yield* range(start + 1, end); > 

Источник

Оцените статью