import { isPlural , plural , singular } from 'pluralize' ; import { Excludes } from '../api' ; export function transformWord ( word , toSingular ) { if ( isPlural ( word ) ) { return toSingular ? singular ( word ) : word ; } else { return toSingular ? word : plural ( word ) ; } } export function optsToArray ( item ) { if ( ! item ) { return [ ] ; } if ( Array . isArray ( item ) ) { return item ; } else { return [ item ] ; } } export function normalizePath ( path , stripTrailing ? ) { if ( typeof path !== 'string' ) { throw new TypeError ( 'expected path to be a string' ) ; } if ( path === '\\' || path === '/' ) { return '/' ; } const len = path . length ; if ( len <= 1 ) { return path ; } let prefix = '' ; if ( len > 4 && path [ 3 ] === '\\' ) { const ch = path [ 2 ] ; if ( ( ch === '?' || ch === '.' ) && path . slice ( 0 , 2 ) === '\\\\' ) { path = path . slice ( 2 ) ; prefix = '//' ; } } const segs = path . split ( /[/\\]+/ ) ; if ( stripTrailing !== false && segs [ segs . length - 1 ] === '' ) { segs . pop ( ) ; } return prefix + segs . join ( '/' ) ; } export function endWithSlash ( path ) { return path . slice ( - 1 ) !== '/' ? `${ path }/` : path ; } export function getName ( path ) { return ( path . split ( '/' ) . pop ( ) || '' ) . replace ( /\..+$/ , '' ) ; } export function chunkName ( cwd , path ) { return stripFirstSlash ( normalizePath ( path ) . replace ( normalizePath ( cwd ) , '' ) ) . replace ( /\//g , '__' ) ; } function stripFirstSlash ( path ) { if ( path . charAt ( 0 ) === '/' ) { return path . slice ( 1 ) ; } else { return path ; } } export function useExclude ( models , excludes ) { return models . filter ( ( model ) => { for ( const exclude of excludes ) { if ( typeof exclude === 'function' && exclude ( getName ( model ) ) ) { return false ; } if ( exclude instanceof RegExp && exclude . test ( getName ( model ) ) ) { return false ; } } return true ; } ) ; }