Skip to content

[RFC]: Improve doctests for ndarray instances in documentation examples (tracking issue) #9329

Open

Description

@kgryte

Instructions

  1. Read the issue description below.
  2. Read the issue comment which follows the description.
  3. Search the code base for a package having a JSDoc example which needs updating according to the description below.
  4. Follow any additional guidance specified in this issue.

Description

This RFC proposes improving doctests for ndarray instances in documentation examples. This is best conveyed through an example.

Consider the following JSDoc example in ndarray/count-falsy:

/**
* ...
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var ndarray = require( '@stdlib/ndarray/ctor' );
*
* // Create a data buffer:
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
*
* // Define the shape of the input array:
* var sh = [ 3, 1, 2 ];
*
* // Define the array strides:
* var sx = [ 4, 4, 1 ];
*
* // Define the index offset:
* var ox = 1;
*
* // Create an input ndarray:
* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' );
*
* // Perform reduction:
* var out = countFalsy( x );
* // returns <ndarray>
*
* var v = out.get();
* // returns 1
*/

Compare to the example after updating in commit 46d9a44

/**
* ...
* 
*
* @example
* var Float64Array = require( '@stdlib/array/float64' );
* var ndarray = require( '@stdlib/ndarray/ctor' );
*
* // Create a data buffer:
* var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 0.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] );
*
* // Define the shape of the input array:
* var sh = [ 3, 1, 2 ];
*
* // Define the array strides:
* var sx = [ 4, 4, 1 ];
*
* // Define the index offset:
* var ox = 1;
*
* // Create an input ndarray:
* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' );
*
* // Perform reduction:
* var out = countFalsy( x );
* // returns <ndarray>[ 1 ]
*/

Notice how, in the updated example, we use the doctest return annotation // returns <ndarray>[ 1 ]. In contrast, in the old example, we explicitly access an ndarray element via the get method.

As may be observed, the updated doctest is much more compact and conveys more clearly the expected behavior.

Further consider the following example in the same package, as documented in the package's README

var array = require( '@stdlib/ndarray/array' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );

// Create an input ndarray:
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
// returns <ndarray>

// Perform reduction:
var out = countFalsy( x, {
    'dims': [ 1, 2 ]
});
// returns <ndarray>

var v = ndarray2array( out );
// returns [ 0, 0, 1 ]

Compare to the example after the same commit linked to above

var array = require( '@stdlib/ndarray/array' );

// Create an input ndarray:
var x = array( [ [ [ 1.0, 2.0 ] ], [ [ 3.0, 4.0 ] ], [ [ 0.0, 6.0 ] ] ] );
// returns <ndarray>

// Perform reduction:
var out = countFalsy( x, {
    'dims': [ 1, 2 ]
});
// returns <ndarray>[ 0, 0, 1 ]

Notice how we removed the explicit usage of ndarray2array in favor of ndarray instance notation <ndarray>[ 0, 0, 1 ], and, given that ndarray2array is no longer used in the example, we also removed the corresponding import (i.e., require( '@stdlib/ndarray/to-array' ).

Accordingly, this RFC seeks to leverage recent improvements in our doctest framework which now supports nested ndarray instance notation for ndarray instances (e.g., <ndarray>[ 1 ]), where previously it did not; hence, the more verbose decomposition logic used in the first example.

Steps

Given the relatively widespread practice of either manually accessing individual ndarray elements or using ndarray2array to convert ndarrays to nested arrays for the sole reason of showing expected results, this RFC aims to be an open call for any contributor wanting to contribute to the project to do the following:

  1. Study the changes made in commit 46d9a44, as this commit contains the sorts of changes that we are looking for.
  2. Find a package containing documentation examples which performs explicit element access or uses ndarray2array in order to show expected values. A possible global project search could use the regular expression var [a-zA-Z0-9]* = (?:ndarray2array)(?:f|)\(. From the search results, you should be able to find a package in need of updating.
  3. Update the examples for that package, and only that package, to migrate to using ndarray instance notation (e.g., <ndarray>[ ... ], etc). Examples may be found in the following package files (note: not all files may require updating; you should inspect each file individually):
    • README.md
    • docs/index.d.ts
    • docs/repl.txt
    • examples/index.js
    • lib/* JSDoc examples
  4. Submit a PR updating the documentation for that package (and only that package).
  5. For the PR title, use the following template "docs: improve doctests for ndarray instances in <package_name>", where <package_name> is the name of the package you updated. For example,
docs: improve doctests for ndarray instances in `ndarray/count-falsy`

Please do NOT make extraneous changes to examples. We are not interested in changing examples wholesale. We are only interested in replacing decomposition logic with ndarray instance notation.

Related Issues

None.

Questions

No.

Other

  • If you are interested in working on this RFC, for each pull request, please only update the examples for a single package.
  • As mentioned above, please do NOT make extraneous changes to examples. We are not interested in changing examples wholesale. Nor are we interested in new "creative" changes. We are only interested in replacing decomposition logic with ndarray instance notation. Failure to match the behavior of the existing examples and to respect this guidance will result in your PRs being automatically closed without review.
  • As this is a "Good First Issue", you are strongly encouraged to avoid using AI when authoring your contribution. One of the primary intents of Good First Issues is to help introduce you to stdlib, its development environment, and the contribution process, as documented in the contributing guide. Most new contributors are unfamiliar with stdlib and its conventions, and thus fail to appropriately use LLMs and AI when authoring contributions, most often generating AI slop and leading to wasted time. Don't be one of those people. :) Take the time to manually author your first several PRs, and, once you are intimately familiar with project conventions, you can consider leveraging AI to augment your dev tasks.

Checklist

  • I have read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.
  • The issue name begins with RFC:.

Activity

  1. added
    RFCRequest for comments. Feature requests and proposed changes.
    DocumentationImprovements, additions, or changes to documentation.
    difficulty: 1Low degree of difficulty. Should be straightforward to implement and/or resolve.
    AcceptedRFC feature request which has been accepted.
    Good First IssueA good first issue for new contributors!
    JavaScriptIssue involves or relates to JavaScript.
    on Dec 23, 2025
  2. stdlib-bot commented on Dec 23, 2025

    @stdlib-bot
    Contributor

    👋 Important: PLEASE READ 👋

    This issue has been labeled as a good first issue and is available for anyone to work on.

    If this is your first time contributing to an open source project, some aspects of the development process may seem unusual, arcane, or some combination of both.

    1. You cannot "claim" issues. People new to open source often want to "claim" or be assigned an issue before beginning work. The typical rationale is that people want to avoid wasted work in the event that someone else ends up working the issue. However, this practice is not effective in open source, as it often leads to "issue squatting", in which an individual asks to be assigned, is granted their request, and then never ends up working on the issue. Accordingly, you are encouraged to communicate your intent to address this issue, ideally by providing a rough outline as to how you plan to address the issue or asking clarifying questions, but, at the end of the day, we will take running code and rough consensus in order to move forward quickly.
    2. We have a very high bar for contributions. We have very high standards for contributions and expect all contributions—whether new features, tests, or documentation—to be rigorous, thorough, and complete. Once a pull request is merged into stdlib, that contribution immediately becomes the collective responsibility of all maintainers of stdlib. When we merge code into stdlib, we are saying that we, the maintainers, commit to reviewing subsequent changes and making bugfixes to the code. Hence, in order to ensure future maintainability, this naturally leads to a higher standard of contribution.

    Before working on this issue and opening a pull request, please read the project's contributing guidelines. These guidelines and the associated development guide provide important information, including links to stdlib's Code of Conduct, license policy, and steps for setting up your local development environment.

    To reiterate, we strongly encourage you to refer to our contributing guides before beginning work on this issue. Failure to follow our guidelines significantly decreases the likelihood that you'll successfully contribute to stdlib and may result in automatic closure of a pull request without review.

    Setting up your local development environment is a critical first step, as doing so ensures that automated development processes for linting, license verification, and unit testing can run prior to authoring commits and pushing changes. If you would prefer to avoid manual setup, we provide pre-configured development containers for use locally or in GitHub Codespaces.

    We place a high value on consistency throughout the stdlib codebase. We encourage you to closely examine other packages in stdlib and attempt to emulate the practices and conventions found therein.

    • If you are attempting to contribute a new package, sometimes the best approach is to simply copy the contents of an existing package and then modify the minimum amount necessary to implement the feature (e.g., changing descriptions, parameter names, and implementation).
    • If you are contributing tests, find a package implementing a similar feature and emulate the tests of that package.
    • If you are updating documentation, examine several similar packages and emulate the content, style, and prose of those packages.

    In short, the more effort you put in to ensure that your contribution looks and feels like stdlib—including variables names, bracket spacing, line breaks, etc—the more likely that your contribution will be reviewed and ultimately accepted. We encourage you to closely study the codebase before beginning work on this issue.

    ✨ Thank you again for your interest in stdlib, and we look forward to reviewing your future contributions. ✨

  3. Youssef-joe commented on Dec 23, 2025

    @Youssef-joe

    Hi! I’m planning to submit a PR addressing this RFC by updating doctests in one package, in line with the referenced commit. Thanks...

  4. 77 remaining items

  5. nightt5879 commented on May 23, 2026

    @nightt5879
    Contributor

    Opened PR #12248 to address this.

  6. AryanSharma48 commented on Aug 11, 2026

    @AryanSharma48
    Contributor

    Migrated JSDoc examples, REPL docs, TypeScript definitions, and README doctests for @stdlib/ndarray/every to use compact instance notation in #14179.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    AcceptedRFC feature request which has been accepted.DocumentationImprovements, additions, or changes to documentation.Good First IssueA good first issue for new contributors!JavaScriptIssue involves or relates to JavaScript.ModernizationIssue or pull request which modernizes stdlib according to the project's most recent conventions.RFCRequest for comments. Feature requests and proposed changes.Tracking IssueTracking issue.difficulty: 1Low degree of difficulty. Should be straightforward to implement and/or resolve.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions