<commit_before>/** api: module = cursor */

/** api: synopsis
 *  Cursor related functionality.
 */

/** api: summary
 *  The :mod:`cursor` module provides a constructor for Cursor objects.
 *
 *  .. code-block:: javascript
 *  
 *      js> var CURSOR = require("geoscript/cursor");
 */

var UTIL = require("./util");

/** api: class = Cursor */
var Cursor = UTIL.extend(Object, {
    
    _cursor: null,
    
    _cast: null,
    
    /** api: [index]
     *  The index of the most recently read result.  This is intialized as
     *  ``-1`` and is incremented with each result read.
     */
    index: -1,
    
    /** api: [property]
     *  ``Object``
     *  The most recently read object in the collection.  This will be ``null``
     *  if no results have been read or if the cursor has been exhausted.
     */
    current: null,
    
    /** api: constructor
     *  .. class:: Cursor
     *
     *      A cursor will not generally need to be created.  Instead, cursors
     *      are returned when querying features from a layer.
     */
    constructor: function(_cursor, _cast) {
        this._cursor = _cursor;
        this._cast = _cast;
    },
    
    /** api: method[hasNext]
     *  :returns: ``Boolean``
     *
     *  Determine whether there are more results to retrieve.
     */
    hasNext: function() {
        var has;
        try {
            has = this._cursor.hasNext();
            if (!has) {
                this.close();
            }
        } catch (err) {
            has = false;
        }
        return has;
    },

    /** api: method[next]
     *  :return: ``Object`` The next result.
     *
     *  Retrieve the next result in the results set.  If no more results are
     *  available, `undefined` will be returned.
     */
    next: function() {
        var results = this.read(1);
        return results[0];
    },
    
    /** api: method[read]
     *  :arg len: ``Number`` Number of results to read.
     *  :returns: ``Array`` An array of results.
     *
     *  Read the next number of results.  The length of the resulting array
     *  will be shorter than the given number in cases where the cursor is
     *  exhausted of results before the given number is reached.
     */
    read: function(len) {
        var _result, results = [];
        for (var i=0; i<len; ++i) {
            try {
                _result = this._cursor.next();
            } catch (err) {
                this.current = null;
                // instead of `throw StopIteration`
                break;
            }
            ++this.index;
            this.current = this._cast(_result);
            results.push(this.current);
        }
        return results;
    },
    
    /** api: method[skip]<commit_msg>Close cursor when exhausted.<commit_after>/** api: module = cursor */

/** api: synopsis
 *  Cursor related functionality.
 */

/** api: summary
 *  The :mod:`cursor` module provides a constructor for Cursor objects.
 *
 *  .. code-block:: javascript
 *  
 *      js> var CURSOR = require("geoscript/cursor");
 */

var UTIL = require("./util");

/** api: class = Cursor */
var Cursor = UTIL.extend(Object, {
    
    _cursor: null,
    
    _cast: null,
    
    /** api: [index]
     *  The index of the most recently read result.  This is intialized as
     *  ``-1`` and is incremented with each result read.
     */
    index: -1,
    
    /** api: [property]
     *  ``Object``
     *  The most recently read object in the collection.  This will be ``null``
     *  if no results have been read or if the cursor has been exhausted.
     */
    current: null,
    
    /** api: constructor
     *  .. class:: Cursor
     *
     *      A cursor will not generally need to be created.  Instead, cursors
     *      are returned when querying features from a layer.
     */
    constructor: function(_cursor, _cast) {
        this._cursor = _cursor;
        this._cast = _cast;
    },
    
    /** api: method[hasNext]
     *  :returns: ``Boolean``
     *
     *  Determine whether there are more results to retrieve.
     */
    hasNext: function() {
        var has;
        try {
            has = this._cursor.hasNext();
            if (!has) {
                this.close();
            }
        } catch (err) {
            has = false;
        }
        return has;
    },

    /** api: method[next]
     *  :return: ``Object`` The next result.
     *
     *  Retrieve the next result in the results set.  If no more results are
     *  available, `undefined` will be returned.
     */
    next: function() {
        var results = this.read(1);
        return results[0];
    },
    
    /** api: method[read]
     *  :arg len: ``Number`` Number of results to read.
     *  :returns: ``Array`` An array of results.
     *
     *  Read the next number of results.  The length of the resulting array
     *  will be shorter than the given number in cases where the cursor is
     *  exhausted of results before the given number is reached.
     */
    read: function(len) {
        var _result, results = [];
        for (var i=0; i<len; ++i) {
            try {
                _result = this._cursor.next();
            } catch (err) {
                this.close();
                this.current = null;
                // instead of `throw StopIteration`
                break;
            }
            ++this.index;
            this.current = this._cast(_result);
            results.push(this.current);
        }
        return results;
    },
    
    /** api: method[skip]
{'fraction_non_alphanumeric': 0.11823284446858591, 'fraction_numerical': 0.001810610175629187, 'mean_word_length': 1.6596051998074146, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3183796', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1580, 'n_words': 529}