f=s=>`
${s}
`.split`
`.map((r,y,s,v=c=>c>' '&c!='-',h=c=>c>' '&c<'|')=>[...r].map((c,x)=>t+=(v(c)?2-v(s[y-1][x])-v(s[y+1][x]):0)+(h(c)?2-h(r[x-1])-h(r[x+1]):0)),t=0)&&t
// Less golfed
u=s=>{
s = ('\n' + s + '\n').split('\n'); // split in rows, adding a blank line at top and one at bottom
t = 0; // init counter
v = c => c>' ' & c!='-'; // function to check if a character has vertical end points
h = c => c>' ' & c<'|'; // function to check if a character has horizontal end points
s.forEach( (r,y) =>
[...r].forEach( (c,x) => {
if (v(c)) // if current character has vertical endpoints, check chars in previous and following row
t += 2 - v(s[y-1][x]) - v(s[y+1][x]);
if (h(c)) // if current character has horizontal endpoints, check previous and following chars in row
t += 2 - h(r[x-1]) - h(r[x+1]);
})
)
return t
}
//TEST
out=x=>O.innerHTML+=x+'\n'
;[
[`+`,4]
,[`-|++-
+`,12]
,[`+--+
| |
+--+`,8]
,[` | |
+--+-- |||`,12]
,[`--++
|||--`,10]
,[``,0]
,[`
|
|`,2]
,[`
--
++--
++
--+
+++ ||
----`,30]
].forEach(t=>{ r=f(t[0]),k=t[1],out('Test '+(r==k?'OK':'Fail')+'\n'+t[0]+'\nResult:'+r+'\nCheck:'+k+'\n') })
<pre id=O></pre>
["",...s.split("\n"),""]
更长的@ETHproductions