<commit_before>/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
*
* 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.
*/

'use strict';

// MODULES //

var path = require( 'path' );
var logger = require( 'debug' );
var isNodeBuiltin = require( '@stdlib/assert/is-node-builtin' );
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
var startsWith = require( '@stdlib/string/starts-with' );
var replace = require( '@stdlib/string/replace' );
var contains = require( '@stdlib/assert/contains' );
var instanceOf = require( '@stdlib/assert/instance-of' );
var pkgDeps = require( '@stdlib/_tools/pkgs/deps' ).sync;
var readJSON = require( '@stdlib/fs/read-json' ).sync;
var standalonePackage = require( './standalone_package.js' );
var prunePackage = require( './prune_package.js' );
var unique = require( './unique.js' );
var getRoot = require( './root.js' );


// VARIABLES //

var debug = logger( 'pkgs:namespace-deps' );
var STDLIB_TYPES = '/// <reference types="@stdlib/types"/>';


// MAIN //

/**
* Returns a list of dependencies for the chosen namespace or package.
*
* @private
* @param {string} ns - namespace or package name
* @param {number} level - desired dependency level
* @param {boolean} dev - boolean indicating whether to return development dependencies instead
* @throws {Error} unable to read `index.d.ts` file when resolving Typescript definitions
* @returns {Array} dependencies array
*/
function namespaceDeps( ns, level, dev ) {
	var namespacePkgs;
	var fileDeps;
	var manifest;
	var entry;
	var conf;
	var deps;
	var name;
	var task;
	var def;
	var pkg;
	var i;
	var j;

	namespacePkgs = pkgDeps( [ ns ], {
		'dev': dev,
		'dir': getRoot( '' )
	})[ 0 ];
	namespacePkgs = ( dev ) ? namespacePkgs.devDeps : namespacePkgs.deps;
	deps = [];
	try {
		entry = path.dirname( require.resolve( ns ) );
		def = readFileSync( path.join( entry, '..', 'docs', 'types', 'index.d.ts' ), 'utf-8' );
		if ( contains( def, STDLIB_TYPES ) && !dev ) {
			deps.push( '@stdlib/types' );
		}
	} catch ( err ) {
		debug( 'Encountered an error while reading `index.d.ts` file: '+err.message );
	}
	manifest = readJSON( path.join( entry, '..', 'manifest.json' ) );
	if ( !instanceOf( manifest, Error ) ) {
		if ( !dev ) {
			name = prunePackage( '@stdlib/utils/library-manifest', level );
			deps.push( standalonePackage( name ) );
		}
		task = manifest.options.task;
		for ( i = 0; i < manifest.confs.length; i++ ) {
			conf = manifest.confs[ i ];
			if ( !dev && conf.task === task ) {
				for ( j = 0; j < conf.dependencies.length; j++ ) {
					if ( !startsWith( conf.dependencies[ j ], ns ) ) {
						name = prunePackage( conf.dependencies[ j ], level );
						deps.push( standalonePackage( name ) );
					}
				}
			} else if ( dev ) {
				for ( j = 0; j < conf.dependencies.length; j++ ) {
					if ( !startsWith( conf.dependencies[ j ], ns ) ) {
						name = prunePackage( conf.dependencies[ j ], level );
						deps.push( standalonePackage( name ) );
					}
				}
			}
		}
	}
	if ( !namespacePkgs || namespacePkgs.length === 0 ) {
		return unique( deps );
	}
	for ( i = 0; i < namespacePkgs.length; i++ ) {
		pkg = namespacePkgs[ i ];
		if ( pkg === ns ) {
			continue;
		}
		if ( startsWith( pkg, ns ) ) {
			fileDeps = namespaceDeps( pkg, level, dev );
			for ( j = 0; j < fileDeps.length; j++ ) {
				if (
					!startsWith( replace( fileDeps[ j ], '/', '-' ), replace( ns, '/', '-' ) ) &&
					fileDeps[ j ] !== pkg<commit_msg>Add special handling for namespace deps of plot<commit_after>/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
*
* 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.
*/

'use strict';

// MODULES //

var path = require( 'path' );
var logger = require( 'debug' );
var isNodeBuiltin = require( '@stdlib/assert/is-node-builtin' );
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
var startsWith = require( '@stdlib/string/starts-with' );
var replace = require( '@stdlib/string/replace' );
var contains = require( '@stdlib/assert/contains' );
var instanceOf = require( '@stdlib/assert/instance-of' );
var pkgDeps = require( '@stdlib/_tools/pkgs/deps' ).sync;
var readJSON = require( '@stdlib/fs/read-json' ).sync;
var standalonePackage = require( './standalone_package.js' );
var prunePackage = require( './prune_package.js' );
var unique = require( './unique.js' );
var getRoot = require( './root.js' );


// VARIABLES //

var debug = logger( 'pkgs:namespace-deps' );
var STDLIB_TYPES = '/// <reference types="@stdlib/types"/>';


// MAIN //

/**
* Returns a list of dependencies for the chosen namespace or package.
*
* @private
* @param {string} ns - namespace or package name
* @param {number} level - desired dependency level
* @param {boolean} dev - boolean indicating whether to return development dependencies instead
* @throws {Error} unable to read `index.d.ts` file when resolving Typescript definitions
* @returns {Array} dependencies array
*/
function namespaceDeps( ns, level, dev ) {
	var namespacePkgs;
	var fileDeps;
	var manifest;
	var entry;
	var conf;
	var deps;
	var name;
	var task;
	var def;
	var pkg;
	var i;
	var j;

	namespacePkgs = pkgDeps( [ ns ], {
		'dev': dev,
		'dir': getRoot( '' )
	})[ 0 ];
	namespacePkgs = ( dev ) ? namespacePkgs.devDeps : namespacePkgs.deps;
	deps = [];
	try {
		entry = path.dirname( require.resolve( ns ) );
		def = readFileSync( path.join( entry, '..', 'docs', 'types', 'index.d.ts' ), 'utf-8' );
		if ( contains( def, STDLIB_TYPES ) && !dev ) {
			deps.push( '@stdlib/types' );
		}
	} catch ( err ) {
		debug( 'Encountered an error while reading `index.d.ts` file: '+err.message );
	}
	manifest = readJSON( path.join( entry, '..', 'manifest.json' ) );
	if ( !instanceOf( manifest, Error ) ) {
		if ( !dev ) {
			name = prunePackage( '@stdlib/utils/library-manifest', level );
			deps.push( standalonePackage( name ) );
		}
		task = manifest.options.task;
		for ( i = 0; i < manifest.confs.length; i++ ) {
			conf = manifest.confs[ i ];
			if ( !dev && conf.task === task ) {
				for ( j = 0; j < conf.dependencies.length; j++ ) {
					if ( !startsWith( conf.dependencies[ j ], ns ) ) {
						name = prunePackage( conf.dependencies[ j ], level );
						deps.push( standalonePackage( name ) );
					}
				}
			} else if ( dev ) {
				for ( j = 0; j < conf.dependencies.length; j++ ) {
					if ( !startsWith( conf.dependencies[ j ], ns ) ) {
						name = prunePackage( conf.dependencies[ j ], level );
						deps.push( standalonePackage( name ) );
					}
				}
			}
		}
	}
	if ( !namespacePkgs || namespacePkgs.length === 0 ) {
		return unique( deps );
	}
	for ( i = 0; i < namespacePkgs.length; i++ ) {
		pkg = namespacePkgs[ i ];
		if ( pkg === ns ) {
			continue;
		}
		if (
			startsWith( pkg, ns ) ||
			( ns === '@stdlib/plot/ctor' && startsWith( pkg, '@stdlib/plot' ) )
		) {
			fileDeps = namespaceDeps( pkg, level, dev );
			for ( j = 0; j < fileDeps.length; j++ ) {
				if (
					!startsWith( replace( fileDeps[ j ], '/', '-' ), replace( ns, '/', '-' ) ) &&
					fileDeps[ j ] !== pkg
{'fraction_non_alphanumeric': 0.15932626325639426, 'fraction_numerical': 0.004491578290704928, 'mean_word_length': 3.2958199356913185, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3955346', 'n_tokens_mistral': 2945, 'n_tokens_neox': 2661, 'n_words': 815}