Garrett Mills
// from the flitter/libflitter project - canon/CanonicalUnit.js
/**
* @module libflitter/canon/CanonicalUnit
*/
const path = require('path')
const rra = require('recursive-readdir-async')
const Unit = require('../Unit')
const error_context = require('../errors/error_context.fn')
const priv = {
// Formats a file path to canonical name.
format_file_name(name) {
try {
const base_dir = process.platform === 'win32' ? this.directory.replace(/\\/g, '/') : this.directory
name = name.replace(base_dir, '')
if (this.suffix) {
const suffix_regexp = new RegExp(`${this.suffix}`, 'g')
name = name.replace(suffix_regexp, '')
}
name = name.replace(/\//g, ':').substring(1)
return name
} catch (e) {
throw error_context(e, {
formatting_file_name: name,
})
}
}
}
/**
* Base class for canonical units that load resources from the filesystem
* and name resources based on the file's location within that system.
* @extends module:libflitter/Unit~Unit
*/
class CanonicalUnit extends Unit {
/**
* Defines the services required by this unit.
* The 'canon' service is provided by default.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'canon']
}
/**
* Get the name of the service provided by this unit.
* @returns {string}
*/
static get name() {
return this.prototype.canonical_item+'s'
}
/**
* Instantiate the unit.
* @param {string} base_directory - the base search directory
*/
// from the flitter/libflitter project - canon/CanonicalUnit.js
/**
* @module libflitter/canon/CanonicalUnit
*/
const path = require('path')
const rra = require('recursive-readdir-async')
const Unit = require('../Unit')
const error_context = require('../errors/error_context.fn')
const priv = {
// Formats a file path to canonical name.
format_file_name(name) {
try {
const base_dir = process.platform === 'win32' ? this.directory.replace(/\\/g, '/') : this.directory
name = name.replace(base_dir, '')
if (this.suffix) {
const suffix_regexp = new RegExp(`${this.suffix}`, 'g')
name = name.replace(suffix_regexp, '')
}
name = name.replace(/\//g, ':').substring(1)
return name
} catch (e) {
throw error_context(e, {
formatting_file_name: name,
})
}
}
}
/**
* Base class for canonical units that load resources from the filesystem
* and name resources based on the file's location within that system.
* @extends module:libflitter/Unit~Unit
*/
class CanonicalUnit extends Unit {
/**
* Defines the services required by this unit.
* The 'canon' service is provided by default.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'canon']
}
/**
* Get the name of the service provided by this unit.
* @returns {string}
*/
static get name() {
return this.prototype.canonical_item+'s'
}
/**
* Instantiate the unit.
* @param {string} base_directory - the base search directory
*/
// from the flitter/libflitter project - canon/CanonicalUnit.js
/**
* @module libflitter/canon/CanonicalUnit
*/
const path = require('path')
const rra = require('recursive-readdir-async')
const Unit = require('../Unit')
const error_context = require('../errors/error_context.fn')
const priv = {
// Formats a file path to canonical name.
format_file_name(name) {
try {
const base_dir = process.platform === 'win32' ? this.directory.replace(/\\/g, '/') : this.directory
name = name.replace(base_dir, '')
if (this.suffix) {
const suffix_regexp = new RegExp(`${this.suffix}`, 'g')
name = name.replace(suffix_regexp, '')
}
name = name.replace(/\//g, ':').substring(1)
return name
} catch (e) {
throw error_context(e, {
formatting_file_name: name,
})
}
}
}
/**
* Base class for canonical units that load resources from the filesystem
* and name resources based on the file's location within that system.
* @extends module:libflitter/Unit~Unit
*/
class CanonicalUnit extends Unit {
/**
* Defines the services required by this unit.
* The 'canon' service is provided by default.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'canon']
}
/**
* Get the name of the service provided by this unit.
* @returns {string}
*/
static get name() {
return this.prototype.canonical_item+'s'
}
/**
* Instantiate the unit.
* @param {string} base_directory - the base search directory
*/
Snippet: pdfsearch
Garrett Mills
Copyright © 2021 Garrett Mills
home
about me
what I'm working on
get in touch
latest updates
résumé
blog
my code
the background