import path from "path" ; import error from "./error" ; function normalizePath ( filename ) { return path . normalize ( filename ) ; } function isInRoot ( root , filename ) { root = path . normalize ( root ) ; filename = path . normalize ( filename ) ; if ( root === "." ) { return true ; } if ( root [ root . length - 1 ] != path . sep ) { root = root + path . sep ; } return filename . substr ( 0 , root . length ) === root ; } function resolveInRoot ( root ) { const args = Array . prototype . slice . call ( arguments , 1 ) ; const input = args . reduce ( ( current , p ) => { if ( p [ 0 ] == "/" || p [ 0 ] == "\\" ) return p . slice ( 1 ) ; return current ? path . join ( current , p ) : path . normalize ( p ) ; } , "" ) ; const result = path . resolve ( root , input ) ; if ( ! isInRoot ( root , result ) ) { throw new error . FileOutOfScopeError ( { filename : result , root : root , } ) ; } return result ; } function setExtension ( filename , ext ) { return path . join ( path . dirname ( filename ) , path . basename ( filename , path . extname ( filename ) ) + ext ) ; } function isPureRelative ( filename ) { return filename . indexOf ( "./" ) === 0 || filename . indexOf ( "../" ) === 0 ; } export default { isInRoot : isInRoot , resolveInRoot : resolveInRoot , normalize : normalizePath , setExtension : setExtension , isPureRelative : isPureRelative , } ;