MCPcopy Create free account
hub / github.com/Effect-TS/effect / parse

Function parse

packages/effect/src/Path.ts:764–841  ·  view source on GitHub ↗
(path)

Source from the content-addressed store, hash-verified

762 },
763
764 parse(path) {
765 const ret = { root: "", dir: "", base: "", ext: "", name: "" }
766 if (path.length === 0) return ret
767 let code = path.charCodeAt(0)
768 const isAbsolute = code === 47 /*/*/
769 let start
770 if (isAbsolute) {
771 ret.root = "/"
772 start = 1
773 } else {
774 start = 0
775 }
776 let startDot = -1
777 let startPart = 0
778 let end = -1
779 let matchedSlash = true
780 let i = path.length - 1
781
782 // Track the state of characters (if any) we see before our first dot and
783 // after any path separator we find
784 let preDotState = 0
785
786 // Get non-dir info
787 for (; i >= start; --i) {
788 code = path.charCodeAt(i)
789 if (code === 47 /*/*/) {
790 // If we reached a path separator that was not part of a set of path
791 // separators at the end of the string, stop now
792 if (!matchedSlash) {
793 startPart = i + 1
794 break
795 }
796 continue
797 }
798 if (end === -1) {
799 // We saw the first non-path separator, mark this as the end of our
800 // extension
801 matchedSlash = false
802 end = i + 1
803 }
804 if (code === 46 /*.*/) {
805 // If this is our first dot, mark it as the start of our extension
806 if (startDot === -1) startDot = i
807 else if (preDotState !== 1) preDotState = 1
808 } else if (startDot !== -1) {
809 // We saw a non-dot and non-path separator before our dot, so we should
810 // have a good chance at having a non-empty extension
811 preDotState = -1
812 }
813 }
814
815 if (
816 startDot === -1 || end === -1 ||
817 // We saw a non-dot character immediately before the dot
818 preDotState === 0 ||
819 // The (right-most) trimmed path component is exactly '..'
820 preDotState === 1 && startDot === end - 1 && startDot === startPart + 1
821 ) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected