MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / is_minified_content

Function is_minified_content

codegraph-kernel/src/textutil.rs:91–131  ·  view source on GitHub ↗

isMinifiedContent (src/extraction/generated-detection.ts): a `.js` / `.mjs` / `.cjs` file of at least 4000 UTF-16 units whose text sits mostly on lines of 1000 units or more, dense with `;{}(),` — or that carries a webpack runtime. Lengths are counted in UTF-16 units, as the JS string ops count them, so both extractors agree on every file.

(file_path: &str, content: &str)

Source from the content-addressed store, hash-verified

89/// runtime. Lengths are counted in UTF-16 units, as the JS string ops count
90/// them, so both extractors agree on every file.
91pub fn is_minified_content(file_path: &str, content: &str) -> bool {
92 const LINE: usize = 1000;
93 let lower = file_path.to_ascii_lowercase();
94 if !(lower.ends_with(".js") || lower.ends_with(".mjs") || lower.ends_with(".cjs")) {
95 return false;
96 }
97 // UTF-16 units never outnumber UTF-8 bytes: a short file is out at once.
98 if content.len() < 4 * LINE {
99 return false;
100 }
101 let total = content.encode_utf16().count();
102 if total < 4 * LINE {
103 return false;
104 }
105 if webpack_runtime().is_match(content) {
106 return true;
107 }
108 let (mut long, mut punctuation) = (0usize, 0usize);
109 let (mut line_len, mut line_punct) = (0usize, 0usize);
110 for unit in content.encode_utf16() {
111 if unit == 10 {
112 if line_len >= LINE {
113 long += line_len;
114 punctuation += line_punct;
115 }
116 line_len = 0;
117 line_punct = 0;
118 continue;
119 }
120 line_len += 1;
121 // ; { } ( ) ,
122 if matches!(unit, 59 | 123 | 125 | 40 | 41 | 44) {
123 line_punct += 1;
124 }
125 }
126 if line_len >= LINE {
127 long += line_len;
128 punctuation += line_punct;
129 }
130 long as f64 >= total as f64 * 0.5 && punctuation as f64 >= long as f64 * 0.03
131}
132
133/// Byte offsets of each line start, for UTF-16 column conversion.
134pub fn line_starts(src: &str) -> Vec<usize> {

Callers 1

extractFunction · 0.85

Calls 1

lenMethod · 0.80

Tested by

no test coverage detected