color-shorthand-hex-to-six-digit
Version:
Convert shorthand hex color codes into full
11 lines (9 loc) • 2.74 kB
JavaScript
/**
* @name color-shorthand-hex-to-six-digit
* @fileoverview Convert shorthand hex color codes into full
* @version 5.2.4
* @author Roy Revelt
* @license MIT
* {@link https://codsen.com/os/color-shorthand-hex-to-six-digit/}
*/
import{isPlainObject as a}from"codsen-utils";var u="5.2.4";var j=u,f=/#(?:[a-f0-9]{3}|[a-f0-9]{4}(?:[a-f0-9]{2}){0,2})\b/gi;function d(e,n){let r=e[n],t=e.charCodeAt(n);return r==="-"||r==="_"||r==="\\"||t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122||t>=128}function h(e,n){let r=[],t=0,o="";for(;t<n;){let s=e[t];if(o){if(s==="\\"){t+=2;continue}s===o&&(o=""),t+=1;continue}if(s==='"'||s==="'"){o=s,t+=1;continue}if(s==="/"&&e[t+1]==="*"){let c=e.indexOf("*/",t+2);if(c===-1||c>=n)break;t=c+2;continue}if(s==="\\"){t+=2;continue}if(s==="("){let c=t;for(;c>0&&d(e,c-1);)c-=1;r.push(e.slice(c,t).toLowerCase())}else s===")"&&r.pop();t+=1}return r.some(s=>s==="url"||s==="src")}function g(e,n){let r=e.lastIndexOf("<",n);if(r<=e.lastIndexOf(">",n))return!1;let t=e.slice(r+1,n);return/(?:^|\s)(?:href|xlink:href)\s*=\s*"[^"]*$/i.test(t)||/(?:^|\s)(?:href|xlink:href)\s*=\s*'[^']*$/i.test(t)||/(?:^|\s)(?:href|xlink:href)\s*=\s*[^\s"'=<>`]*$/i.test(t)}function y(e,n){return e.lastIndexOf("(",n)===-1&&e.lastIndexOf("<",n)===-1?!1:h(e,n)||g(e,n)}function b(e,n){if(e.indexOf("{",n)===-1)return!1;let r="",t=0;for(;t<n;){let o=e[t];if(r){if(o==="\\"){t+=2;continue}o===r&&(r=""),t+=1;continue}if(o==='"'||o==="'"){r=o,t+=1;continue}if(o==="/"&&e[t+1]==="*"){let s=e.indexOf("*/",t+2);if(s===-1||s>=n)return!1;t=s+2;continue}t+=1}for(;n<e.length;){let o=e[n];if(r){if(o==="\\"){n+=2;continue}o===r&&(r=""),n+=1;continue}if(o==='"'||o==="'"){r=o,n+=1;continue}if(o==="/"&&e[n+1]==="*"){let s=e.indexOf("*/",n+2);if(s===-1)return!1;n=s+2;continue}if(o==="{"||o==="}"||o===";")return o==="{";n+=1}return!1}function m(e,n,r){let t=n+e.length;if(r[n-1]==="&"||d(r,t)||y(r,n)||b(r,t))return e;if(e.length===4){let s=e[1].toLowerCase(),c=e[2].toLowerCase(),l=e[3].toLowerCase();return`#${s}${s}${c}${c}${l}${l}`}return e.toLowerCase()}function x(e,n,r){n==="__proto__"?Object.defineProperty(e,n,{configurable:!0,enumerable:!0,value:r,writable:!0}):e[n]=r}function i(e,n){if(typeof e=="string")return e.replace(f,m);if(Array.isArray(e)){let r=n.get(e);if(r!==void 0)return r;let t=new Array(e.length);n.set(e,t);for(let o=0;o<e.length;o+=1)o in e&&(t[o]=i(e[o],n));return t}if(a(e)){let r=n.get(e);if(r!==void 0)return r;let t=Object.create(Object.getPrototypeOf(e));n.set(e,t);for(let o of Reflect.ownKeys(e))Object.prototype.propertyIsEnumerable.call(e,o)&&x(t,o,i(e[o],n));return t}return e}function v(e){return typeof e=="string"?e.replace(f,m):!Array.isArray(e)&&!a(e)?e:i(e,new WeakMap)}export{v as conv,j as version};