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

Function extname

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

Source from the content-addressed store, hash-verified

704 },
705
706 extname(path) {
707 let startDot = -1
708 let startPart = 0
709 let end = -1
710 let matchedSlash = true
711 // Track the state of characters (if any) we see before our first dot and
712 // after any path separator we find
713 let preDotState = 0
714 for (let i = path.length - 1; i >= 0; --i) {
715 const code = path.charCodeAt(i)
716 if (code === 47 /*/*/) {
717 // If we reached a path separator that was not part of a set of path
718 // separators at the end of the string, stop now
719 if (!matchedSlash) {
720 startPart = i + 1
721 break
722 }
723 continue
724 }
725 if (end === -1) {
726 // We saw the first non-path separator, mark this as the end of our
727 // extension
728 matchedSlash = false
729 end = i + 1
730 }
731 if (code === 46 /*.*/) {
732 // If this is our first dot, mark it as the start of our extension
733 if (startDot === -1) {
734 startDot = i
735 } else if (preDotState !== 1) {
736 preDotState = 1
737 }
738 } else if (startDot !== -1) {
739 // We saw a non-dot and non-path separator before our dot, so we should
740 // have a good chance at having a non-empty extension
741 preDotState = -1
742 }
743 }
744
745 if (
746 startDot === -1 || end === -1 ||
747 // We saw a non-dot character immediately before the dot
748 preDotState === 0 ||
749 // The (right-most) trimmed path component is exactly '..'
750 preDotState === 1 && startDot === end - 1 && startDot === startPart + 1
751 ) {
752 return ""
753 }
754 return path.slice(startDot, end)
755 },
756
757 format: function format(pathObject) {
758 if (pathObject === null || typeof pathObject !== "object") {

Callers 1

fileResponseFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected