@waiting/shared-core
Advanced tools
Comparing version 1.8.0 to 1.9.0
@@ -5,2 +5,11 @@ # Changelog | ||
## [1.9.0](https://github.com/waitingsong/node-shared-core/compare/v1.8.0...v1.9.0) (2019-05-21) | ||
### Features | ||
* add ab2str(), str2ab(), genRandomInt() ([70d20e8](https://github.com/waitingsong/node-shared-core/commit/70d20e8)) | ||
## [1.8.0](https://github.com/waitingsong/node-shared-core/compare/v1.7.0...v1.8.0) (2019-05-21) | ||
@@ -7,0 +16,0 @@ |
@@ -5,3 +5,3 @@ /** | ||
* | ||
* @version 1.8.0 | ||
* @version 1.9.0 | ||
* @author waiting | ||
@@ -217,3 +217,5 @@ * @license MIT | ||
/** | ||
* Convert Browser ArrayBuffer to js Buffer | ||
* Convert js ArrayBuffer to Node.js Buffer | ||
* | ||
* @see https://stackoverflow.com/a/14737423 | ||
*/ | ||
@@ -225,2 +227,4 @@ function ab2buf(ab) { | ||
* Convert Node.js Buffer to js ArrayBuffer | ||
* | ||
* @see https://stackoverflow.com/a/14737423 | ||
*/ | ||
@@ -230,3 +234,39 @@ function buf2ab(buf) { | ||
} | ||
/** | ||
* Convert ArrayBuffer to String via TextDecoder | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder | ||
*/ | ||
function ab2str(ab, encoding = 'utf8') { | ||
const decoder = new TextDecoder(encoding); | ||
const view = new Uint8Array(ab); | ||
return decoder.decode(view) | ||
} | ||
/** | ||
* Convert String to ArrayBuffer via TextEncoder | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder | ||
*/ | ||
function str2ab(input) { | ||
const view = str2Uint8Array(input); | ||
return view.buffer | ||
} | ||
/** Convert String to Uint8Array */ | ||
function str2Uint8Array(input) { | ||
const encoder = new TextEncoder(); | ||
const view = encoder.encode(input); | ||
return view | ||
} | ||
/** | ||
* Generate random integer | ||
* | ||
* @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/random | ||
*/ | ||
function genRandomInt(max) { | ||
return Math.floor(Math.random() * Math.floor(max)) | ||
} | ||
/* istanbul ignore next */ | ||
@@ -380,2 +420,3 @@ const isWin32 = process.platform === 'win32' ? true : false; | ||
exports.ab2buf = ab2buf; | ||
exports.ab2str = ab2str; | ||
exports.assertNever = assertNever; | ||
@@ -393,2 +434,3 @@ exports.assertNeverRx = assertNeverRx; | ||
exports.genFileFromExample = genFileFromExample; | ||
exports.genRandomInt = genRandomInt; | ||
exports.isDirExists = isDirExists; | ||
@@ -409,2 +451,4 @@ exports.isDirFileExists = isDirFileExists; | ||
exports.statAsync = statAsync; | ||
exports.str2Uint8Array = str2Uint8Array; | ||
exports.str2ab = str2ab; | ||
exports.unlinkAsync = unlinkAsync; | ||
@@ -411,0 +455,0 @@ exports.userHome = userHome; |
@@ -5,3 +5,3 @@ /** | ||
* | ||
* @version 1.8.0 | ||
* @version 1.9.0 | ||
* @author waiting | ||
@@ -215,3 +215,5 @@ * @license MIT | ||
/** | ||
* Convert Browser ArrayBuffer to js Buffer | ||
* Convert js ArrayBuffer to Node.js Buffer | ||
* | ||
* @see https://stackoverflow.com/a/14737423 | ||
*/ | ||
@@ -223,2 +225,4 @@ function ab2buf(ab) { | ||
* Convert Node.js Buffer to js ArrayBuffer | ||
* | ||
* @see https://stackoverflow.com/a/14737423 | ||
*/ | ||
@@ -228,3 +232,39 @@ function buf2ab(buf) { | ||
} | ||
/** | ||
* Convert ArrayBuffer to String via TextDecoder | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder | ||
*/ | ||
function ab2str(ab, encoding = 'utf8') { | ||
const decoder = new TextDecoder(encoding); | ||
const view = new Uint8Array(ab); | ||
return decoder.decode(view) | ||
} | ||
/** | ||
* Convert String to ArrayBuffer via TextEncoder | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder | ||
*/ | ||
function str2ab(input) { | ||
const view = str2Uint8Array(input); | ||
return view.buffer | ||
} | ||
/** Convert String to Uint8Array */ | ||
function str2Uint8Array(input) { | ||
const encoder = new TextEncoder(); | ||
const view = encoder.encode(input); | ||
return view | ||
} | ||
/** | ||
* Generate random integer | ||
* | ||
* @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/random | ||
*/ | ||
function genRandomInt(max) { | ||
return Math.floor(Math.random() * Math.floor(max)) | ||
} | ||
/* istanbul ignore next */ | ||
@@ -335,2 +375,2 @@ const isWin32 = process.platform === 'win32' ? true : false; | ||
export { ab2buf, assertNever, assertNeverRx, buf2ab, chmodAsync, closeAsync, copyFileAsync, createDir, createDirAsync, createFileAsync, dirExists, fileExists, genFileFromExample, isDirExists, isDirFileExists, isFileExists, isPathAccessible, isSkipCommitlint, isWin32, mkdirAsync, openAsync, pathAccessible, readDirAsync, readFileAsync, rimraf, rmdirAsync, setPathDirectory, statAsync, unlinkAsync, userHome, validateDllFile, writeAsync, writeFileAsync }; | ||
export { ab2buf, ab2str, assertNever, assertNeverRx, buf2ab, chmodAsync, closeAsync, copyFileAsync, createDir, createDirAsync, createFileAsync, dirExists, fileExists, genFileFromExample, genRandomInt, isDirExists, isDirFileExists, isFileExists, isPathAccessible, isSkipCommitlint, isWin32, mkdirAsync, openAsync, pathAccessible, readDirAsync, readFileAsync, rimraf, rmdirAsync, setPathDirectory, statAsync, str2Uint8Array, str2ab, unlinkAsync, userHome, validateDllFile, writeAsync, writeFileAsync }; |
@@ -5,3 +5,3 @@ /** | ||
* | ||
* @version 1.8.0 | ||
* @version 1.9.0 | ||
* @author waiting | ||
@@ -12,3 +12,3 @@ * @license MIT | ||
import{throwError as t,defer as e,of as n,from as r,combineLatest as o,forkJoin as i}from"rxjs";import{close as s,chmod as a,copyFile as c,mkdir as f,open as p,readFile as u,readdir as l,rmdir as w,stat as m,unlink as h,write as v,writeFile as d,access as x}from"fs";import{normalize as y,sep as E,resolve as g,dirname as M,join as I}from"path";export{basename,dirname,join,normalize,resolve as pathResolve}from"path";import{map as P,mergeMap as b,scan as T,concatMap as j,last as D,mapTo as O,defaultIfEmpty as $,tap as A,share as F,filter as R,take as S}from"rxjs/operators";import{promisify as B}from"util";export{promisify}from"util";export{tmpdir}from"os";function N(t){throw new Error("Assert Never Unexpected object: "+t)}function U(e){return t(new Error("Assert Never Unexpected object: "+e))}const C=B(s),G=B(a),H=B(c),_=B(f),k=B(p),L=B(u),z=B(l),J=B(w),q=B(m),K=B(h),Q=B(v),V=B(d);function W(t){return e(()=>X(t)).pipe(P(e=>e?y(t):""))}function X(t){return t?new Promise(e=>x(t,t=>e(!t))):Promise.resolve(!1)}function Y(t){if(!t)return n("");const r=y(t);return e(()=>Z(r)).pipe(P(t=>t?r:""))}function Z(t){return t?nt(t,"DIR"):Promise.resolve(!1)}function tt(t){const n=y(t);return e(()=>et(n)).pipe(P(t=>t?n:""))}function et(t){return t?nt(t,"FILE"):Promise.resolve(!1)}function nt(t,e){return t?X(t).then(n=>!!n&&q(t).then(t=>"DIR"===e?t.isDirectory():t.isFile())):Promise.resolve(!1)}function rt(t){if(!t)throw new Error("value of path param invalid");const e=n(y(t)),o=e.pipe(b(t=>r(t.split(E))),T((t,e)=>g(t,e),E)),i=o.pipe(j(ot),D()),s=e.pipe(b(Y),b(t=>t?n(t):i));return s}function ot(t,r){return W(t).pipe(b(r=>r?n(r):e(()=>_(t,493)).pipe(O(t))))}async function it(t){if(t){const e=y(t);return await Z(e)||await e.split(E).reduce(async(t,e)=>{const n=g(await t,e);return await X(n)||await _(n,493),n},Promise.resolve(E)),e}throw new Error("value of path param invalid")}async function st(t,e,n){const r=M(t);if(!r)throw new Error("folder empty");await Z(r)||await it(r);const o=y(t);if(!await et(o)){const t=n||{mode:416};Buffer.isBuffer(e)?await V(o,e,t):"object"==typeof e?await V(o,JSON.stringify(e)):await V(o,e,t)}return o}async function at(t){t&&(await ct(t),await Z(t)&&await J(t))}async function ct(t){if(t&&await X(t)){if(await et(t))return void await K(t);const e=await z(t);if(e.length)for(const n of e)await ct(I(t,n));else await J(t)}}async function ft(t){if(!t)throw new Error("File path empth ");if(t.includes("/")&&!await X(t))throw new Error("File not exists: "+t)}function pt(t){return Buffer.from(t)}function ut(t){return Uint8Array.from(t).buffer}const lt="win32"===process.platform,wt=y(lt?process.env.USERPROFILE||"":`${process.env.HOME}`);function mt(t){t&&"string"==typeof t&&process&&process.env&&(process.env.PATH=`${process.env.PATH};${t}`)}async function ht(t,e){const n=[];for(const r of e){const e=I(t,r.replace(/\.{2,}/,"/"));if(!await X(e))continue;const o=await z(e);for(const t of o){if(!vt(t))continue;const o=I(e,t),i=dt(t),s=I(e,i);await X(s)||(await H(o,s),n.push(`${r}/${i}`))}}return n}function vt(t){if(!t)return!1;if(".example"===t)return!1;const e=t.split(".");return e.length>1&&"example"===e[e.length-1]}function dt(t){const e=t.split(".");return e.length>1&&"example"===e[e.length-1]?e.slice(0,e.length-1).join("."):t}function xt(t){const{baseDir:e,COMMIT_EDITMSG:s,branchName:a,protectBranch:c,skipMsg:f}=t,p=I(e,s);if(!s)return console.info("COMMIT_EDITMSG value blank"),n(1);const u=r(c).pipe($()),l=r(f).pipe($()),w=W(p).pipe(A(t=>{t||(console.info(`COMMIT_EDITMSG file not exists: "${p}"`),process.exit(1))}),b(t=>L(t,{encoding:"utf8"})),P(t=>{const e=t.split(/\n|\r\n/)[0];return{head:e,msg:t}}),F()),m=o(n(a),u).pipe(P(([t,e])=>!(!e||!e.test(t))),R(t=>t),O(0),$(1)),h=o(w,l).pipe(P(([{head:t},e])=>!(!e||!e.test(t))),R(t=>t),O(1),$(0)),v=i(m,h).pipe(P(([t,e])=>t&e),$(0),S(1));return v}export{pt as ab2buf,N as assertNever,U as assertNeverRx,ut as buf2ab,G as chmodAsync,C as closeAsync,H as copyFileAsync,rt as createDir,it as createDirAsync,st as createFileAsync,Y as dirExists,tt as fileExists,ht as genFileFromExample,Z as isDirExists,nt as isDirFileExists,et as isFileExists,X as isPathAccessible,xt as isSkipCommitlint,lt as isWin32,_ as mkdirAsync,k as openAsync,W as pathAccessible,z as readDirAsync,L as readFileAsync,at as rimraf,J as rmdirAsync,mt as setPathDirectory,q as statAsync,K as unlinkAsync,wt as userHome,ft as validateDllFile,Q as writeAsync,V as writeFileAsync}; | ||
import{throwError as t,defer as e,of as n,from as r,combineLatest as o,forkJoin as i}from"rxjs";import{close as s,chmod as a,copyFile as c,mkdir as f,open as p,readFile as u,readdir as l,rmdir as w,stat as m,unlink as h,write as d,writeFile as v,access as x}from"fs";import{normalize as y,sep as E,resolve as M,dirname as g,join as I}from"path";export{basename,dirname,join,normalize,resolve as pathResolve}from"path";import{map as T,mergeMap as b,scan as P,concatMap as D,last as j,mapTo as A,defaultIfEmpty as O,tap as $,share as F,filter as R,take as S}from"rxjs/operators";import{promisify as U}from"util";export{promisify}from"util";export{tmpdir}from"os";function B(t){throw new Error("Assert Never Unexpected object: "+t)}function N(e){return t(new Error("Assert Never Unexpected object: "+e))}const C=U(s),G=U(a),H=U(c),_=U(f),k=U(p),L=U(u),z=U(l),J=U(w),q=U(m),K=U(h),Q=U(d),V=U(v);function W(t){return e(()=>X(t)).pipe(T(e=>e?y(t):""))}function X(t){return t?new Promise(e=>x(t,t=>e(!t))):Promise.resolve(!1)}function Y(t){if(!t)return n("");const r=y(t);return e(()=>Z(r)).pipe(T(t=>t?r:""))}function Z(t){return t?nt(t,"DIR"):Promise.resolve(!1)}function tt(t){const n=y(t);return e(()=>et(n)).pipe(T(t=>t?n:""))}function et(t){return t?nt(t,"FILE"):Promise.resolve(!1)}function nt(t,e){return t?X(t).then(n=>!!n&&q(t).then(t=>"DIR"===e?t.isDirectory():t.isFile())):Promise.resolve(!1)}function rt(t){if(!t)throw new Error("value of path param invalid");const e=n(y(t)),o=e.pipe(b(t=>r(t.split(E))),P((t,e)=>M(t,e),E)),i=o.pipe(D(ot),j()),s=e.pipe(b(Y),b(t=>t?n(t):i));return s}function ot(t,r){return W(t).pipe(b(r=>r?n(r):e(()=>_(t,493)).pipe(A(t))))}async function it(t){if(t){const e=y(t);return await Z(e)||await e.split(E).reduce(async(t,e)=>{const n=M(await t,e);return await X(n)||await _(n,493),n},Promise.resolve(E)),e}throw new Error("value of path param invalid")}async function st(t,e,n){const r=g(t);if(!r)throw new Error("folder empty");await Z(r)||await it(r);const o=y(t);if(!await et(o)){const t=n||{mode:416};Buffer.isBuffer(e)?await V(o,e,t):"object"==typeof e?await V(o,JSON.stringify(e)):await V(o,e,t)}return o}async function at(t){t&&(await ct(t),await Z(t)&&await J(t))}async function ct(t){if(t&&await X(t)){if(await et(t))return void await K(t);const e=await z(t);if(e.length)for(const n of e)await ct(I(t,n));else await J(t)}}async function ft(t){if(!t)throw new Error("File path empth ");if(t.includes("/")&&!await X(t))throw new Error("File not exists: "+t)}function pt(t){return Buffer.from(t)}function ut(t){return Uint8Array.from(t).buffer}function lt(t,e="utf8"){const n=new TextDecoder(e),r=new Uint8Array(t);return n.decode(r)}function wt(t){const e=mt(t);return e.buffer}function mt(t){const e=new TextEncoder,n=e.encode(t);return n}function ht(t){return Math.floor(Math.random()*Math.floor(t))}const dt="win32"===process.platform,vt=y(dt?process.env.USERPROFILE||"":`${process.env.HOME}`);function xt(t){t&&"string"==typeof t&&process&&process.env&&(process.env.PATH=`${process.env.PATH};${t}`)}async function yt(t,e){const n=[];for(const r of e){const e=I(t,r.replace(/\.{2,}/,"/"));if(!await X(e))continue;const o=await z(e);for(const t of o){if(!Et(t))continue;const o=I(e,t),i=Mt(t),s=I(e,i);await X(s)||(await H(o,s),n.push(`${r}/${i}`))}}return n}function Et(t){if(!t)return!1;if(".example"===t)return!1;const e=t.split(".");return e.length>1&&"example"===e[e.length-1]}function Mt(t){const e=t.split(".");return e.length>1&&"example"===e[e.length-1]?e.slice(0,e.length-1).join("."):t}function gt(t){const{baseDir:e,COMMIT_EDITMSG:s,branchName:a,protectBranch:c,skipMsg:f}=t,p=I(e,s);if(!s)return console.info("COMMIT_EDITMSG value blank"),n(1);const u=r(c).pipe(O()),l=r(f).pipe(O()),w=W(p).pipe($(t=>{t||(console.info(`COMMIT_EDITMSG file not exists: "${p}"`),process.exit(1))}),b(t=>L(t,{encoding:"utf8"})),T(t=>{const e=t.split(/\n|\r\n/)[0];return{head:e,msg:t}}),F()),m=o(n(a),u).pipe(T(([t,e])=>!(!e||!e.test(t))),R(t=>t),A(0),O(1)),h=o(w,l).pipe(T(([{head:t},e])=>!(!e||!e.test(t))),R(t=>t),A(1),O(0)),d=i(m,h).pipe(T(([t,e])=>t&e),O(0),S(1));return d}export{pt as ab2buf,lt as ab2str,B as assertNever,N as assertNeverRx,ut as buf2ab,G as chmodAsync,C as closeAsync,H as copyFileAsync,rt as createDir,it as createDirAsync,st as createFileAsync,Y as dirExists,tt as fileExists,yt as genFileFromExample,ht as genRandomInt,Z as isDirExists,nt as isDirFileExists,et as isFileExists,X as isPathAccessible,gt as isSkipCommitlint,dt as isWin32,_ as mkdirAsync,k as openAsync,W as pathAccessible,z as readDirAsync,L as readFileAsync,at as rimraf,J as rmdirAsync,xt as setPathDirectory,q as statAsync,mt as str2Uint8Array,wt as str2ab,K as unlinkAsync,vt as userHome,ft as validateDllFile,Q as writeAsync,V as writeFileAsync}; | ||
//# sourceMappingURL=index.esm.min.js.map |
@@ -71,3 +71,5 @@ /// <reference types="node" /> | ||
/** | ||
* Convert Browser ArrayBuffer to js Buffer | ||
* Convert js ArrayBuffer to Node.js Buffer | ||
* | ||
* @see https://stackoverflow.com/a/14737423 | ||
*/ | ||
@@ -77,3 +79,25 @@ export declare function ab2buf(ab: ArrayBuffer): Buffer; | ||
* Convert Node.js Buffer to js ArrayBuffer | ||
* | ||
* @see https://stackoverflow.com/a/14737423 | ||
*/ | ||
export declare function buf2ab(buf: Buffer): ArrayBuffer; | ||
/** | ||
* Convert ArrayBuffer to String via TextDecoder | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder | ||
*/ | ||
export declare function ab2str(ab: ArrayBuffer, encoding?: 'utf8' | 'utf16'): string; | ||
/** | ||
* Convert String to ArrayBuffer via TextEncoder | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder | ||
*/ | ||
export declare function str2ab(input: string): ArrayBuffer; | ||
/** Convert String to Uint8Array */ | ||
export declare function str2Uint8Array(input: string): Uint8Array; | ||
/** | ||
* Generate random integer | ||
* | ||
* @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/random | ||
*/ | ||
export declare function genRandomInt(max: number): number; |
@@ -195,3 +195,5 @@ import { access, chmod, close, copyFile, mkdir, open, readdir, readFile, rmdir, stat, unlink, write, writeFile, } from 'fs' | ||
/** | ||
* Convert Browser ArrayBuffer to js Buffer | ||
* Convert js ArrayBuffer to Node.js Buffer | ||
* | ||
* @see https://stackoverflow.com/a/14737423 | ||
*/ | ||
@@ -203,2 +205,4 @@ export function ab2buf(ab) { | ||
* Convert Node.js Buffer to js ArrayBuffer | ||
* | ||
* @see https://stackoverflow.com/a/14737423 | ||
*/ | ||
@@ -208,1 +212,37 @@ export function buf2ab(buf) { | ||
} | ||
/** | ||
* Convert ArrayBuffer to String via TextDecoder | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder | ||
*/ | ||
export function ab2str(ab, encoding = 'utf8') { | ||
const decoder = new TextDecoder(encoding) | ||
const view = new Uint8Array(ab) | ||
return decoder.decode(view) | ||
} | ||
/** | ||
* Convert String to ArrayBuffer via TextEncoder | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder | ||
*/ | ||
export function str2ab(input) { | ||
const view = str2Uint8Array(input) | ||
return view.buffer | ||
} | ||
/** Convert String to Uint8Array */ | ||
export function str2Uint8Array(input) { | ||
const encoder = new TextEncoder() | ||
const view = encoder.encode(input) | ||
return view | ||
} | ||
/** | ||
* Generate random integer | ||
* | ||
* @see https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Math/random | ||
*/ | ||
export function genRandomInt(max) { | ||
return Math.floor(Math.random() * Math.floor(max)) | ||
} |
{ | ||
"name": "@waiting/shared-core", | ||
"author": "waiting", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"description": "node core function re export with Promise or Observable", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73195
1291