Comparing version 11.0.3 to 11.0.4
58
build.js
@@ -6,3 +6,3 @@ /** | ||
import { mkdirSync, readFileSync, rmSync, writeFileSync, copyFileSync } from 'fs' | ||
import { mkdirSync, readFileSync, rmSync } from 'fs' | ||
@@ -21,8 +21,7 @@ import { buildSync } from 'esbuild' | ||
const comment = [ | ||
`// ${pkg.name}@${pkg.version}, by ${pkg.author}`, | ||
`built with esbuild at ${buildTime}`, | ||
`published under ${pkg.license} license` | ||
`// ${pkg.name}@${pkg.version} ${pkg.repository.url}`, | ||
`built with esbuild at ${buildTime}` | ||
].join(' - ') | ||
const baseOpt = { | ||
const esmVersion = { | ||
entryPoints: ['src/main.js'], | ||
@@ -33,10 +32,6 @@ bundle: true, | ||
minify: true, | ||
write: true | ||
} | ||
const esmVersion = { | ||
...baseOpt, | ||
platform: 'neutral', | ||
write: true, | ||
platform: 'browser', | ||
format: 'esm', | ||
mainFields: ['module'], | ||
sourcemap: 'external', | ||
outfile: 'dist/bella.esm.js', | ||
@@ -48,40 +43,1 @@ banner: { | ||
buildSync(esmVersion) | ||
const cjsVersion = { | ||
...baseOpt, | ||
platform: 'node', | ||
format: 'cjs', | ||
mainFields: ['main'], | ||
outfile: 'dist/cjs/bella.js', | ||
banner: { | ||
js: comment | ||
} | ||
} | ||
buildSync(cjsVersion) | ||
const cjspkg = { | ||
name: pkg.name + '-cjs', | ||
version: pkg.version, | ||
main: './bella.js' | ||
} | ||
writeFileSync( | ||
'dist/cjs/package.json', | ||
JSON.stringify(cjspkg, null, ' '), | ||
'utf8' | ||
) | ||
const iifeVersion = { | ||
...baseOpt, | ||
platform: 'browser', | ||
format: 'iife', | ||
mainFields: ['browser'], | ||
target: ['es2020'], | ||
globalName: 'bella', | ||
outfile: 'dist/bella.iife.js', | ||
banner: { | ||
js: comment | ||
} | ||
} | ||
buildSync(iifeVersion) | ||
// For backward | ||
copyFileSync(iifeVersion.outfile, './dist/bella.min.js') |
@@ -12,6 +12,2 @@ // release.test | ||
const esmFile = 'bella.esm.js' | ||
const cjsFile = 'cjs/bella.js' | ||
const minFile = 'bella.min.js' | ||
const runtest = (fname) => { | ||
@@ -27,4 +23,3 @@ const fpath = `./dist/${fname}` | ||
expect(lines[0].includes(`${pkg.name}@${pkg.version}`)).toBeTruthy() | ||
expect(lines[0].includes(pkg.author)).toBeTruthy() | ||
expect(lines[0].includes(pkg.license)).toBeTruthy() | ||
expect(lines[0].includes(pkg.repository.url)).toBeTruthy() | ||
}) | ||
@@ -35,10 +30,2 @@ }) | ||
const arr = [ | ||
esmFile, | ||
cjsFile, | ||
minFile | ||
] | ||
arr.forEach((fname) => { | ||
runtest(fname) | ||
}) | ||
runtest('bella.esm.js') |
@@ -1,2 +0,2 @@ | ||
// bellajs@11.0.3, by @ndaidong - built with esbuild at 2022-06-03T15:04:16.581Z - published under MIT license | ||
var h=t=>({}).toString.call(t),F=t=>Number.isInteger(t),u=t=>Array.isArray(t),p=t=>String(t)===t,y=t=>Number(t)===t,R=t=>Boolean(t)===t,N=t=>h(t)==="[object Null]",_=t=>h(t)==="[object Undefined]",T=t=>_(t)||N(t),z=t=>h(t)==="[object Function]",l=t=>h(t)==="[object Object]"&&!u(t),m=t=>t instanceof Date&&!isNaN(t.valueOf()),B=t=>h(t).match(/^\[object HTML\w*Element]$/)!==null,H=t=>{let e=/^[a-z]+$/i;return p(t)&&e.test(t)},$=t=>{let e=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;return p(t)&&e.test(t)},w=t=>!t||T(t)||p(t)&&t===""||u(t)&&t.length===0||l(t)&&Object.keys(t).length===0,a=(t,e)=>!t||!e?!1:Object.prototype.hasOwnProperty.call(t,e);var S=(t,e)=>{if(w(t)&&w(e))return!0;if(m(t)&&m(e))return t.getTime()===e.getTime();if(u(t)&&u(e)){if(t.length!==e.length)return!1;let r=!0;for(let n=0;n<t.length;n++)if(!S(t[n],e[n])){r=!1;break}return r}if(l(t)&&l(e)){if(Object.keys(t).length!==Object.keys(e).length)return!1;let r=!0;for(let n in t)if(!a(e,n)||!S(t[n],e[n])){r=!1;break}return r}return t===e};var D=Number.MAX_SAFE_INTEGER,A=(t,e)=>{if((!t||t<0)&&(t=0),e||(e=D),t===e)return e;t>e&&(t=Math.min(t,e),e=Math.max(t,e));let r=t,n=e-t+1;return Math.floor(Math.random()*n)+r};var g=t=>{let e=y(t)?String(t):t;if(!p(e))throw new Error("InvalidInput: String required.");return e},K=(t,e)=>{let r=g(t),n=e||140;if(r.length<=n)return r;let o=r.substring(0,n),s=o.split(" "),i=s.length,c="";return i>1?(s.pop(),c+=s.join(" "),c.length<r.length&&(c+="...")):(o=o.substring(0,n-3),c=o+"..."),c},Q=t=>g(t).replace(/<.*?>/gi," ").replace(/\s\s+/g," ").trim(),Z=t=>g(t).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,"""),v=t=>g(t).replace(/"/g,'"').replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&"),k=t=>{let e=g(t).toLowerCase();return e.length>1?e.charAt(0).toUpperCase()+e.slice(1):e.toUpperCase()},tt=t=>g(t).split(" ").map(e=>k(e)).join(" "),j=(t,e,r)=>{let n=g(t);if(y(e)&&(e=String(e)),y(r)&&(r=String(r)),p(e)&&p(r))n=n.split(e).join(r);else if(u(e)&&p(r))e.forEach(o=>{n=j(n,o,r)});else if(u(e)&&u(r)&&e.length===r.length){let o=e.length;if(o>0)for(let s=0;s<o;s++){let i=e[s],c=r[s];n=j(n,i,c)}}return n},I=t=>{let e=g(t),r={a:"á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|ä",A:"Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ|Ä",c:"ç",C:"Ç",d:"đ",D:"Đ",e:"é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|ë",E:"É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ|Ë",i:"í|ì|ỉ|ĩ|ị|ï|î",I:"Í|Ì|Ỉ|Ĩ|Ị|Ï|Î",o:"ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|ö",O:"Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ô|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ|Ö",u:"ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|û",U:"Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự|Û",y:"ý|ỳ|ỷ|ỹ|ỵ",Y:"Ý|Ỳ|Ỷ|Ỹ|Ỵ"},n=(o,s)=>{e=j(e,o,s)};for(let o in r)a(r,o)&&r[o].split("|").forEach(i=>n(i,o));return e},et=(t,e="")=>{let r="abcdefghijklmnopqrstuvwxyz",n=r.toUpperCase(),s=[r,n,"0123456789"].join("").split("").sort(()=>Math.random()>.5).join(""),i=s.length,c=Math.max(t||32,e.length),f=e;for(;f.length<c;){let E=A(0,i);f+=s.charAt(E)||""}return f},rt=(t,e="-")=>I(t).trim().toLowerCase().replace(/\W+/g," ").replace(/\s+/g," ").replace(/\s/g,e);var O={dateStyle:"medium",timeStyle:"long"},d={second:1e3,minute:60,hour:60,day:24,week:7,month:4,year:12},L=t=>{try{return new Intl.Locale(t).language!==""}catch{return!1}},st=(...t)=>{let e=t[0],r=L(t[1])?t[1]:"en",n=t.length>=3?t[2]:t.length===1?O:l(t[1])?t[1]:O;return new Intl.DateTimeFormat(r,n).format(new Date(e))},ct=(t,e="en",r="just now")=>{let n=new Date(t),o=Date.now()-n;if(o<=d.second)return r;let s="second";for(let c in d){if(o<d[c])break;s=c,o/=d[c]}return o=Math.floor(o),new Intl.RelativeTimeFormat(e).format(-o,s)};var ut=t=>{let e=t.length,r=(n,o)=>n>0?(...s)=>r(n-s.length,[...o,...s]):t(...o);return r(e,[])};var pt=(...t)=>t.reduce((e,r)=>n=>e(r(n)));var at=(...t)=>t.reduce((e,r)=>n=>r(e(n)));var x=(t,e,r,n={})=>{let{writable:o=!1,configurable:s=!1,enumerable:i=!1}=n;Object.defineProperty(t,e,{value:r,writable:o,configurable:s,enumerable:i})};var b=t=>{let e=t,r=()=>e==null,n=()=>e,o=f=>b(e||f()),s=f=>b(f(e)===!0?e:null),i=f=>b(r()?null:f(e)),c=Object.create({});return x(c,"__value__",e,{enumerable:!0}),x(c,"__type__","Maybe",{enumerable:!0}),x(c,"isNil",r),x(c,"value",n),x(c,"map",i),x(c,"if",s),x(c,"else",o),c};var M=(t,e=null)=>{let r=e||new Set;if(r.has(t))return t;if(r.add(t),m(t))return new Date(t.valueOf());let n=s=>{let i=Object.create({});for(let c in s)a(s,c)&&(i[c]=M(s[c],r));return i},o=s=>[...s].map(i=>u(i)?o(i):l(i)?n(i):M(i,r));return u(t)?o(t):l(t)?n(t):t},P=(t,e,r=!1,n=[])=>{for(let o in t)if(!(n.length>0&&n.includes(o))&&(!r||r&&a(e,o))){let s=t[o],i=e[o];l(i)&&l(s)||u(i)&&u(s)?e[o]=P(s,e[o],r,n):e[o]=M(s)}return e},dt=(t=[])=>[...new Set(t)],C=(t,e)=>t>e?1:t<e?-1:0,U=(t=[],e=null)=>{let r=[...t],n=e||C;return r.sort(n),r},wt=(t=[],e=1,r="")=>!p(r)||!a(t[0],r)?t:U(t,(n,o)=>n[r]>o[r]?e:n[r]<o[r]?-1*e:0),q=(t=[])=>{let e=[...t],r=[],n=e.length;for(;n>0;){let o=Math.floor(Math.random()*n);r.push(e.splice(o,1)[0]),n--}return r},jt=(t=[],e=1)=>{let r=q(t),n=Math.max(1,e),o=Math.min(n,r.length-1);return r.splice(0,o)};export{M as clone,pt as compose,P as copies,ut as curry,S as equals,Z as escapeHTML,st as formatDateString,ct as formatTimeAgo,et as genid,a as hasProperty,u as isArray,R as isBoolean,m as isDate,B as isElement,$ as isEmail,w as isEmpty,z as isFunction,F as isInteger,H as isLetter,T as isNil,N as isNull,y as isNumber,l as isObject,p as isString,_ as isUndefined,b as maybe,jt as pick,at as pipe,A as randint,j as replaceAll,q as shuffle,rt as slugify,U as sort,wt as sortBy,I as stripAccent,Q as stripTags,K as truncate,k as ucfirst,tt as ucwords,v as unescapeHTML,dt as unique}; | ||
// bellajs@11.0.4 https://github.com/ndaidong/bellajs - built with esbuild at 2022-07-29T03:34:48.099Z | ||
var h=t=>({}).toString.call(t),R=t=>Number.isInteger(t),u=t=>Array.isArray(t),p=t=>String(t)===t,m=t=>Number(t)===t,z=t=>Boolean(t)===t,E=t=>h(t)==="[object Null]",O=t=>h(t)==="[object Undefined]",N=t=>O(t)||E(t),B=t=>h(t)==="[object Function]",l=t=>h(t)==="[object Object]"&&!u(t),j=t=>t instanceof Date&&!isNaN(t.valueOf()),H=t=>h(t).match(/^\[object HTML\w*Element]$/)!==null,$=t=>{let e=/^[a-z]+$/i;return p(t)&&e.test(t)},V=t=>{let e=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;return p(t)&&e.test(t)},X=t=>!t||N(t)||p(t)&&t===""||u(t)&&t.length===0||l(t)&&Object.keys(t).length===0,g=(t,e)=>!t||!e?!1:Object.prototype.hasOwnProperty.call(t,e);var _=Number.MAX_SAFE_INTEGER,M=(t,e)=>{if((!t||t<0)&&(t=0),e||(e=_),t===e)return e;t>e&&(t=Math.min(t,e),e=Math.max(t,e));let r=t,o=e-t+1;return Math.floor(Math.random()*o)+r};var a=t=>{let e=m(t)?String(t):t;if(!p(e))throw new Error("InvalidInput: String required.");return e},K=(t,e)=>{let r=a(t),o=e||140;if(r.length<=o)return r;let n=r.substring(0,o),s=n.split(" "),c=s.length,i="";return c>1?(s.pop(),i+=s.join(" "),i.length<r.length&&(i+="...")):(n=n.substring(0,o-3),i=n+"..."),i},Q=t=>a(t).replace(/<.*?>/gi," ").replace(/\s\s+/g," ").trim(),Z=t=>a(t).replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,"""),v=t=>a(t).replace(/"/g,'"').replace(/</g,"<").replace(/>/g,">").replace(/&/g,"&"),D=t=>{let e=a(t).toLowerCase();return e.length>1?e.charAt(0).toUpperCase()+e.slice(1):e.toUpperCase()},tt=t=>a(t).split(" ").map(e=>D(e)).join(" "),w=(t,e,r)=>{let o=a(t);if(m(e)&&(e=String(e)),m(r)&&(r=String(r)),p(e)&&p(r))o=o.split(e).join(r);else if(u(e)&&p(r))e.forEach(n=>{o=w(o,n,r)});else if(u(e)&&u(r)&&e.length===r.length){let n=e.length;if(n>0)for(let s=0;s<n;s++){let c=e[s],i=r[s];o=w(o,c,i)}}return o},T=t=>{let e=a(t),r={a:"á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|ä",A:"Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ|Ä",c:"ç",C:"Ç",d:"đ",D:"Đ",e:"é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|ë",E:"É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ|Ë",i:"í|ì|ỉ|ĩ|ị|ï|î",I:"Í|Ì|Ỉ|Ĩ|Ị|Ï|Î",o:"ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|ö",O:"Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ô|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ|Ö",u:"ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|û",U:"Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự|Û",y:"ý|ỳ|ỷ|ỹ|ỵ",Y:"Ý|Ỳ|Ỷ|Ỹ|Ỵ"},o=(n,s)=>{e=w(e,n,s)};for(let n in r)g(r,n)&&r[n].split("|").forEach(c=>o(c,n));return e},S="abcdefghijklmnopqrstuvwxyz",I=S.toUpperCase(),C="0123456789",L=[S,I,C].join("").split(""),et=(t,e="")=>{let r=L.sort(()=>Math.random()>.5).join(""),o=r.length,n=Math.max(t||32,e.length),s=e;for(;s.length<n;){let c=M(0,o);s+=r.charAt(c)||""}return s},rt=(t,e="-")=>T(t).trim().toLowerCase().replace(/\W+/g," ").replace(/\s+/g," ").replace(/\s/g,e);var A={dateStyle:"medium",timeStyle:"long"},d={second:1e3,minute:60,hour:60,day:24,week:7,month:4,year:12},P=t=>{try{return new Intl.Locale(t).language!==""}catch{return!1}},st=(...t)=>{let e=t[0],r=P(t[1])?t[1]:"en",o=t.length>=3?t[2]:t.length===1?A:l(t[1])?t[1]:A;return new Intl.DateTimeFormat(r,o).format(new Date(e))},ct=(t,e="en",r="just now")=>{let o=new Date(t),n=Date.now()-o;if(n<=d.second)return r;let s="second";for(let i in d){if(n<d[i])break;s=i,n/=d[i]}return n=Math.floor(n),new Intl.RelativeTimeFormat(e).format(-n,s)};var ut=t=>{let e=t.length,r=(o,n)=>o>0?(...s)=>r(o-s.length,[...n,...s]):t(...n);return r(e,[])};var lt=(...t)=>t.reduce((e,r)=>o=>e(r(o)));var ft=(...t)=>t.reduce((e,r)=>o=>r(e(o)));var f=(t,e,r,o={})=>{let{writable:n=!1,configurable:s=!1,enumerable:c=!1}=o;Object.defineProperty(t,e,{value:r,writable:n,configurable:s,enumerable:c})};var y=t=>{let e=t,r=()=>e==null,o=()=>e,n=x=>y(e||x()),s=x=>y(x(e)===!0?e:null),c=x=>y(r()?null:x(e)),i=Object.create({});return f(i,"__value__",e,{enumerable:!0}),f(i,"__type__","Maybe",{enumerable:!0}),f(i,"isNil",r),f(i,"value",o),f(i,"map",c),f(i,"if",s),f(i,"else",n),i};var b=(t,e=null)=>{let r=e||new Set;if(r.has(t))return t;if(r.add(t),j(t))return new Date(t.valueOf());let o=s=>{let c=Object.create({});for(let i in s)g(s,i)&&(c[i]=b(s[i],r));return c},n=s=>[...s].map(c=>u(c)?n(c):l(c)?o(c):b(c,r));return u(t)?n(t):l(t)?o(t):t},U=(t,e,r=!1,o=[])=>{for(let n in t)if(!(o.length>0&&o.includes(n))&&(!r||r&&g(e,n))){let s=t[n],c=e[n];l(c)&&l(s)||u(c)&&u(s)?e[n]=U(s,e[n],r,o):e[n]=b(s)}return e},wt=(t=[])=>[...new Set(t)],k=(t,e)=>t>e?1:t<e?-1:0,F=(t=[],e=null)=>{let r=[...t],o=e||k;return r.sort(o),r},yt=(t=[],e=1,r="")=>!p(r)||!g(t[0],r)?t:F(t,(o,n)=>o[r]>n[r]?e:o[r]<n[r]?-1*e:0),q=(t=[])=>{let e=[...t],r=[],o=e.length;for(;o>0;){let n=Math.floor(Math.random()*o);r.push(e.splice(n,1)[0]),o--}return r},bt=(t=[],e=1)=>{let r=q(t),o=Math.max(1,e),n=Math.min(o,r.length-1);return r.splice(0,n)};export{b as clone,lt as compose,U as copies,ut as curry,Z as escapeHTML,st as formatDateString,ct as formatTimeAgo,et as genid,g as hasProperty,u as isArray,z as isBoolean,j as isDate,H as isElement,V as isEmail,X as isEmpty,B as isFunction,R as isInteger,$ as isLetter,N as isNil,E as isNull,m as isNumber,l as isObject,p as isString,O as isUndefined,y as maybe,bt as pick,ft as pipe,M as randint,w as replaceAll,q as shuffle,rt as slugify,F as sort,yt as sortBy,T as stripAccent,Q as stripTags,K as truncate,D as ucfirst,tt as ucwords,v as unescapeHTML,wt as unique}; |
{ | ||
"version": "11.0.3", | ||
"version": "11.0.4", | ||
"name": "bellajs", | ||
@@ -11,5 +11,3 @@ "description": "A useful helper for any javascript program", | ||
"author": "@ndaidong", | ||
"main": "./dist/cjs/bella.js", | ||
"module": "./src/main.js", | ||
"browser": "./dist/bella.esm.js", | ||
"main": "./src/main.js", | ||
"type": "module", | ||
@@ -22,3 +20,3 @@ "engines": { | ||
"pretest": "npm run lint", | ||
"test": "NODE_ENV=test NODE_OPTIONS=--experimental-vm-modules jest --verbose --coverage=true --unhandled-rejections=strict --detectOpenHandles --env=jsdom", | ||
"test": "NODE_ENV=test NODE_OPTIONS=--experimental-vm-modules jest --verbose --coverage=true --env=jsdom", | ||
"build": "node build.js src/main.js", | ||
@@ -28,6 +26,5 @@ "reset": "node reset" | ||
"devDependencies": { | ||
"esbuild": "^0.14.42", | ||
"jest": "^28.1.0", | ||
"jest-environment-jsdom": "^28.1.0", | ||
"standard": "^17.0.0" | ||
"esbuild": "^0.14.51", | ||
"jest": "^28.1.3", | ||
"jest-environment-jsdom": "^28.1.3" | ||
}, | ||
@@ -34,0 +31,0 @@ "standard": { |
272
README.md
@@ -20,7 +20,7 @@ BellaJS | ||
* [String manipulation](#string-manipulation) | ||
* [Data handling](#data-handling): [clone](#clone), [copies](#copies) | ||
* [Array utils](#array-utils): [pick](#pick), [sort](#sort), [sortBy](#sortBy), [shuffle](#shuffle), [unique](#unique) | ||
* [Functional utils](#functional-utils): [curry](#curryfn), [compose](#compose), [pipe](#pipe), [maybe](#maybe) | ||
* [Date utils](#date-utils) | ||
* [Other utils](#other-utils): [equals](#equals), [randint](#randint), [genid](#genid) | ||
* [Data handling](#data-handling): [`clone`](#cloneanything-val), [`copies`](#copiesobject-source-object-target-boolean-requirematching-array-excepts) | ||
* [Array utils](#array-utils): [`pick`](#pickarray-arr--number-count--1), [`sort`](#sortarray-arr--function-compare), [`sortBy`](#sortbyarray-arr-number-order-string-property), [`shuffle`](#shufflearray-arr), [`unique`](#uniquearray-arr) | ||
* [Functional utils](#functional-utils): [`curry`](#curryfn), [`compose`](#composef1-f2-fn), [`pipe`](#pipef1-f2-fn), [`maybe`](#maybeanything-val) | ||
* [Date utils](#date-utils): [`formatDateString`](#formatdatestringdate--timestamp--string-locale--object-options), [`formatTimeAgo`](#formattimeagodate--timestamp--string-locale--string-justnow) | ||
* [Other utils](#other-utils): [`randint`](#randintnumber-min--number-max), [`genid`](#genidnumber-length--string-prefix) | ||
@@ -31,50 +31,22 @@ * [Test](#test) | ||
## Setup | ||
## Install | ||
- Node.js | ||
```bash | ||
npm i bellajs | ||
```bash | ||
npm i bellajs | ||
# pnpm | ||
pnpm i bellajs | ||
# pnpm | ||
pnpm i bellajs | ||
# yarn | ||
yarn add bellajs | ||
``` | ||
- CDN | ||
- ES6 Module: [bella.esm.js](https://unpkg.com/bellajs/dist/bella.esm.js) | ||
- CommonJS: [bella.js](https://unpkg.com/bellajs/dist/cjs/bella.js) | ||
- For old browsers: [bella.min.js](https://unpkg.com/bellajs/dist/bella.min.js) | ||
## Usage | ||
### Node.js: | ||
```js | ||
import { | ||
slugify | ||
} from 'bella' | ||
// with CommonJS environment | ||
// const { genid, slugify } = require('bellajs/dist/cjs/bella.js') | ||
slugify('') | ||
# yarn | ||
yarn add bellajs | ||
``` | ||
##### Note: | ||
- Browser | ||
> Since Node.js v14, ECMAScript modules [have became the official standard format](https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_modules_ecmascript_modules). | ||
> Just ensure that you are [using module system](https://nodejs.org/api/packages.html#determining-module-system) and enjoy with ES6 import/export syntax. | ||
### Browsers: | ||
Currently ECMAScript modules work fine on almost browsers: | ||
```html | ||
<script type="module"> | ||
import { genid } from 'https://unpkg.com/bellajs/dist/bella.esm.js' | ||
console.log(genid()) | ||
@@ -84,10 +56,15 @@ </script> | ||
With outdated browsers, we can use traditional way: | ||
```html | ||
<script type="text/javascript" src="https://unpkg.com/bellajs/dist/bella.iife.js"></script> | ||
## Usage | ||
<script> | ||
console.log(window.bella.genid()) | ||
</script> | ||
```js | ||
import { | ||
genid, | ||
slugify | ||
} from 'bella' | ||
const postId = genid(32) | ||
console.log(postId) | ||
const slug = slugify('Goldman Sachs, JPMorgan Predict Euro-Area Recession') | ||
console.log(slug) | ||
``` | ||
@@ -99,17 +76,17 @@ | ||
- .isArray(Anything val) | ||
- .isBoolean(Anything val) | ||
- .isDate(Anything val) | ||
- .isElement(Anything val) | ||
- .isEmail(Anything val) | ||
- .isEmpty(Anything val) | ||
- .isFunction(Anything val) | ||
- .isInteger(Anything val) | ||
- .isLetter(Anything val) | ||
- .isNil(Anything val) | ||
- .isNull(Anything val) | ||
- .isNumber(Anything val) | ||
- .isObject(Anything val) | ||
- .isString(Anything val) | ||
- .isUndefined(Anything val) | ||
- `.isArray(Anything val)` | ||
- `.isBoolean(Anything val)` | ||
- `.isDate(Anything val)` | ||
- `.isElement(Anything val)` | ||
- `.isEmail(Anything val)` | ||
- `.isEmpty(Anything val)` | ||
- `.isFunction(Anything val)` | ||
- `.isInteger(Anything val)` | ||
- `.isLetter(Anything val)` | ||
- `.isNil(Anything val)` | ||
- `.isNull(Anything val)` | ||
- `.isNumber(Anything val)` | ||
- `.isObject(Anything val)` | ||
- `.isString(Anything val)` | ||
- `.isUndefined(Anything val)` | ||
@@ -119,11 +96,11 @@ | ||
- .ucfirst(String s) | ||
- .ucwords(String s) | ||
- .escapeHTML(String s) | ||
- .unescapeHTML(String s) | ||
- .slugify(String s) | ||
- .stripTags(String s) | ||
- .stripAccent(String s) | ||
- .truncate(String s, Number limit) | ||
- .replaceAll(String s, String|Array search, String|Array replace) | ||
- `.ucfirst(String s)` | ||
- `.ucwords(String s)` | ||
- `.escapeHTML(String s)` | ||
- `.unescapeHTML(String s)` | ||
- `.slugify(String s)` | ||
- `.stripTags(String s)` | ||
- `.stripAccent(String s)` | ||
- `.truncate(String s, Number limit)` | ||
- `.replaceAll(String s, String|Array search, String|Array replace)` | ||
@@ -133,11 +110,9 @@ | ||
#### clone | ||
#### `clone(Anything val)` | ||
Make a deep copy of a variable. | ||
```js | ||
clone(Anything val) | ||
``` | ||
import { clone } from 'bellajs' | ||
Return a copy of val. | ||
```js | ||
const b = [ | ||
@@ -151,3 +126,3 @@ 1, 5, 0, 'a', -10, '-10', '', | ||
const cb = bella.clone(b) | ||
const cb = clone(b) | ||
console.log(cb) | ||
@@ -174,15 +149,14 @@ ``` | ||
#### copies | ||
#### `copies(Object source, Object target[[, Boolean requireMatching], Array excepts])` | ||
Copy the properties from *source* to *target*. | ||
```js | ||
copies(Object source, Object target[[, Boolean requireMatching], Array excepts]) | ||
``` | ||
- *requireMatching*: if true, BellaJS only copies the properties that are already exist in *target*. | ||
- *excepts*: array of the properties properties in *source* that you don't want to copy. | ||
After this action, target will be modified. | ||
```js | ||
import { copies } from 'bellajs' | ||
const a = { | ||
@@ -209,3 +183,3 @@ name: 'Toto', | ||
bella.copies(a, b) | ||
copies(a, b) | ||
console.log(b) | ||
@@ -236,3 +210,3 @@ ``` | ||
#### pick | ||
#### `pick(Array arr [, Number count = 1])` | ||
@@ -242,8 +216,2 @@ Randomly choose N elements from array. | ||
```js | ||
pick(Array arr [, Integer count = 1]) | ||
``` | ||
Examples: | ||
```js | ||
import { pick } from 'bellajs' | ||
@@ -259,10 +227,6 @@ | ||
#### sort | ||
#### `sort(Array arr [, Function compare])` | ||
```js | ||
sort(Array a [, Function compare]) | ||
``` | ||
Sort the array using a function. | ||
For example: | ||
```js | ||
@@ -278,12 +242,7 @@ import { sort } from 'bellajs' | ||
#### sortBy | ||
#### `sortBy(Array arr, Number order, String property)` | ||
```js | ||
sortBy(Array a, Number order, String property) | ||
``` | ||
Sort the array by specific property and direction. | ||
For example: | ||
```js | ||
import { sortBy } from 'bellajs' | ||
@@ -318,13 +277,7 @@ | ||
#### shuffle | ||
#### `shuffle(Array arr)` | ||
Shuffle an array. | ||
Shuffle the positions of elements in an array. | ||
```js | ||
shuffle(Array arr) | ||
``` | ||
For example: | ||
```js | ||
import { shuffle } from 'bellajs' | ||
@@ -335,10 +288,6 @@ | ||
#### unique | ||
#### `unique(Array arr)` | ||
```js | ||
unique(Array a) | ||
``` | ||
Remove all duplicate elements from an array. | ||
For example: | ||
```js | ||
@@ -352,10 +301,6 @@ import { unique } from 'bellajs' | ||
#### curry | ||
#### `curry(fn)` | ||
```js | ||
curry(fn) | ||
``` | ||
Make a curried function. | ||
Examples: | ||
```js | ||
@@ -375,3 +320,3 @@ import { curry } from 'bellajs' | ||
#### compose | ||
#### `compose(f1, f2, ...fN)` | ||
@@ -381,10 +326,4 @@ Performs right-to-left function composition. | ||
```js | ||
compose(f1, f2, ...fN) | ||
``` | ||
import { compose } from 'bellajs' | ||
Examples: | ||
```js | ||
import {compose} from 'bellajs' | ||
const f1 = (name) => { | ||
@@ -418,3 +357,3 @@ return `f1 ${name}` | ||
#### pipe | ||
#### `pipe(f1, f2, ...fN)` | ||
@@ -424,8 +363,2 @@ Performs left-to-right function composition. | ||
```js | ||
pipe(f1, f2, ...fN) | ||
``` | ||
Examples: | ||
```js | ||
import { pipe } from 'bellajs' | ||
@@ -460,12 +393,6 @@ | ||
#### maybe | ||
#### `maybe(Anything val)` | ||
```js | ||
maybe(Anything val) | ||
``` | ||
Return a static variant of `Maybe` monad. | ||
Examples: | ||
```js | ||
@@ -516,12 +443,7 @@ import { maybe } from 'bellajs' | ||
- `formatDateString(Date | Timestamp [, String locale [, Object options]])` | ||
- `formatTimeAgo(Date | Timestamp [, String locale [, String justnow]])` | ||
#### `formatDateString(Date | Timestamp [, String locale [, Object options]])` | ||
Example: | ||
```js | ||
import { | ||
formatDateString, | ||
formatTimeAgo | ||
formatDateString | ||
} from 'bellajs' | ||
@@ -555,3 +477,14 @@ | ||
}) // => 20:34:28 Giờ Đông Dương Thứ Hai, 3 tháng 1, 2022 | ||
``` | ||
#### `formatTimeAgo(Date | Timestamp [, String locale [, String justnow]])` | ||
```js | ||
import { | ||
formatTimeAgo | ||
} from 'bellajs' | ||
const today = new Date() | ||
const yesterday = today.setDate(today.getDate() - 1) | ||
@@ -590,26 +523,7 @@ formatTimeAgo(yesterday) // => 1 day ago | ||
#### equals | ||
#### `randint([Number min [, Number max]])` | ||
```js | ||
equals(Anything a, Anything b) | ||
``` | ||
Returns a number between `min` and `max` | ||
Examples: | ||
```js | ||
import { equals } from 'bellajs' | ||
equals({}, {}) // => true | ||
equals(0, 1) // => false | ||
``` | ||
#### randint | ||
```js | ||
randint([Number min [, Number max]]) | ||
``` | ||
Examples: | ||
```js | ||
import { randint } from 'bellajs' | ||
@@ -621,10 +535,6 @@ | ||
#### genid | ||
#### `genid([Number length [, String prefix]])` | ||
```js | ||
genid([Number length [, String prefix]]) | ||
``` | ||
Create random ID string. | ||
Examples: | ||
```js | ||
@@ -631,0 +541,0 @@ import { genid } from 'bellajs' |
@@ -121,3 +121,2 @@ /** | ||
export * from './utils/detection.js' | ||
export * from './utils/equals.js' | ||
export * from './utils/string.js' | ||
@@ -124,0 +123,0 @@ export * from './utils/random.js' |
@@ -144,15 +144,16 @@ // utils / string | ||
const lowerChars = 'abcdefghijklmnopqrstuvwxyz' | ||
const upperChars = lowerChars.toUpperCase() | ||
const digits = '0123456789' | ||
const characters = [ | ||
lowerChars, | ||
upperChars, | ||
digits | ||
].join('').split('') | ||
export const genid = (leng, prefix = '') => { | ||
const lc = 'abcdefghijklmnopqrstuvwxyz' | ||
const uc = lc.toUpperCase() | ||
const nb = '0123456789' | ||
const cand = [ | ||
lc, | ||
uc, | ||
nb | ||
].join('').split('').sort(() => { | ||
const chars = characters.sort(() => { | ||
return Math.random() > 0.5 | ||
}).join('') | ||
const t = cand.length | ||
const t = chars.length | ||
const ln = Math.max(leng || 32, prefix.length) | ||
@@ -162,3 +163,3 @@ let s = prefix | ||
const k = randint(0, t) | ||
s += cand.charAt(k) || '' | ||
s += chars.charAt(k) || '' | ||
} | ||
@@ -165,0 +166,0 @@ return s |
3
79966
29
1465
536