Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // src/model_base.js
- 'use strict'
- import _ from '../lodash-mixins'
- const utils = {}
- /**
- * Retrieve the file name of the model importing/requiring this file
- *
- * @return {string} Full path and filename of parent file - EG: /path/File-Name.js
- */
- utils.getParentFilename = () => {
- try {
- var err = new Error()
- var callerfile
- var currentfile
- Error.prepareStackTrace = (err, stack) => stack
- currentfile = err.stack.shift().getFileName()
- while (err.stack.length) {
- callerfile = err.stack.shift().getFileName()
- if(currentfile !== callerfile)
- return callerfile
- }
- } catch (err) {}
- return undefined
- }
- /**
- * Return a model name based off of the filename/path returned from getParentFilename().
- * Basically just a regex match for the filename, without the extension, and converted
- * to snakeCase
- *
- * @return {string} Parent file name converted to string - EG: /path/File-Name.js -> file_name
- */
- utils.getModelName = () => {
- const parentFile = utils.getParentFilename()
- if( ! parentFile)
- throw new Error('Failed to get the filename of the model calling the base model')
- return _.snakeCase( parentFile.match(/\/([a-zA-Z0-9]+)\.js/)[1] )
- }
- module.exports = utils
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement