Skip to content

Commit

Permalink
Move to separate file and add id check
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Oct 24, 2022
1 parent 4efafc7 commit 6c8d511
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 51 deletions.
2 changes: 1 addition & 1 deletion lib/node_modules/@stdlib/_tools/links/validate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
-->

# validate
# validateLinks

> Validate an array of link objects.
Expand Down
27 changes: 2 additions & 25 deletions lib/node_modules/@stdlib/_tools/links/validate/lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var format = require( '@stdlib/string/format' );
var cwd = require( '@stdlib/process/cwd' );
var config = require( './defaults.js' );
var validate = require( './validate.js' );
var check = require( './check.js' );


// MAIN //
Expand Down Expand Up @@ -98,35 +99,11 @@ function validateLinks( links, options, clbk ) {
* @returns {void}
*/
function onRead( error, db ) {
var link;
var out;
var url;
var id;
var i;
if ( error ) {
return cb( error );
}
out = [];
for ( i = 0; i < links.length; i++ ) {
link = links[ i ];
url = link.url;
id = link.id;
if ( !db[ url ] ) {
out.push({
'error': 'Entry not found in database',
'id': id,
'url': url
});
}
else if ( db[ url ].id !== id ) {
out.push({
'error': 'Mismatched link identifier',
'id': id,
'url': url,
'expected': db[ url ].id
});
}
}
out = check( links, db );
cb( null, out );
}
}
Expand Down
103 changes: 103 additions & 0 deletions lib/node_modules/@stdlib/_tools/links/validate/lib/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2022 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 objectEntries = require( '@stdlib/utils/entries' );


// VARIABLES //

var idToUrl = null;


// FUNCTIONS //

/**
* Creates a mapping from link identifiers to link URLs and stores the mapping in variable `idToUrl`.
*
* @private
* @param {Object} db - link database mapping link URLs to metadata
*/
function createIdToUrl( db ) {
var entries;
var i;

idToUrl = {};
entries = objectEntries( db );
for ( i = 0; i < entries.length; i++ ) {
idToUrl[ entries[ i ][ 1 ].id ] = entries[ i ][ 0 ];
}
}


// MAIN //

/**
* Validates an array of link objects against a link database.
*
* @private
* @param {ObjectArray} links - array of link objects to validate
* @param {Object} db - link database mapping link URLs to metadata
* @returns {ObjectArray} array of validation errors
*/
function check( links, db ) {
var link;
var out;
var msg;
var url;
var id;
var i;

out = [];
for ( i = 0; i < links.length; i++ ) {
link = links[ i ];
url = link.url;
id = link.id;
if ( !db[ url ] ) {
if ( !idToUrl ) {
createIdToUrl( db );
}
msg = 'Entry not found in database';
if ( idToUrl[ id ] ) {
msg += '. Did you mean to use URL `'+idToUrl[ id ]+'`?';
}
out.push({
'error': msg,
'id': id,
'url': url
});
}
else if ( db[ url ].id !== id ) {
out.push({
'error': 'Mismatched link identifier',
'id': id,
'url': url,
'expected': db[ url ].id
});
}
}
return out;
}


// EXPORTS //

module.exports = check;
27 changes: 2 additions & 25 deletions lib/node_modules/@stdlib/_tools/links/validate/lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var format = require( '@stdlib/string/format' );
var cwd = require( '@stdlib/process/cwd' );
var config = require( './defaults.js' );
var validate = require( './validate.js' );
var check = require( './check.js' );


// MAIN //
Expand Down Expand Up @@ -55,13 +56,9 @@ var validate = require( './validate.js' );
function validateLinks( links, options ) {
var fopts;
var opts;
var link;
var url;
var err;
var out;
var db;
var id;
var i;
if ( !isObjectArray( links ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an array of objects. Value: `%s`.', links ) );
}
Expand All @@ -82,27 +79,7 @@ function validateLinks( links, options ) {
if ( instanceOf( db, Error ) ) {
return db;
}
out = [];
for ( i = 0; i < links.length; i++ ) {
link = links[ i ];
url = link.url;
id = link.id;
if ( !db[ url ] ) {
out.push({
'message': 'Entry not found in database',
'id': id,
'url': url
});
}
else if ( db[ url ].id !== id ) {
out.push({
'message': 'Mismatched link identifier',
'id': id,
'url': url,
'expected': db[ url ].id
});
}
}
out = check( links, db );
return out;
}

Expand Down

0 comments on commit 6c8d511

Please sign in to comment.