WSH (Windows Script Host) utility library (similar to Node.js-Util, Lodash) and the core module for tuckn/Wsh series.
WshModeJs
└─ WshZLIB
└─ WshNet
└─ WshChildProcess
└─ WshProcess
└─ WshFileSystem
└─ WshOS
└─ WshPath
└─ WshUtil - This repository
└─ WshPolyfill
The upper layer module can use all the functions of the lower layer module.
Works on JScript in Windows.
(1) Create a directory of your WSH project.
D:\> mkdir MyWshProject
D:\> cd MyWshProject
(2) Download this ZIP and unzip or Use the following git
command.
> git clone https://github.com/tuckn/WshUtil.git ./WshModules/WshUtil
or
> git submodule add https://github.com/tuckn/WshUtil.git ./WshModules/WshUtil
(3) Create your JScript (.js) file. For Example,
D:\MyWshProject\
├─ MyScript.js <- Your JScript code will be written in this.
└─ WshModules\
└─ WshUtil\
└─ dist\
└─ bundle.js
I recommend JScript (.js) file encoding to be UTF-8 [BOM, CRLF].
(4) Create your WSF packaging scripts file (.wsf).
D:\MyWshProject\
├─ Run.wsf <- WSH entry file
├─ MyScript.js
└─ WshModules\
└─ WshUtil\
└─ dist\
└─ bundle.js
And you should include .../dist/bundle.js into the WSF file. For Example, The content of the above Run.wsf is
<package>
<job id = "run">
<script language="JScript" src="./WshModules/WshUtil/dist/bundle.js"></script>
<script language="JScript" src="./MyScript.js"></script>
</job>
</package>
I recommend this WSH file (.wsf) encoding to be UTF-8 [BOM, CRLF].
Awesome! This WSH configuration allows you to use the following functions in JScript (.\MyScript.js).
Now you can use Wsh
object in .\MyScript.js (JScript).
for example,
var _ = Wsh.Util; // Shorthand
// Checks deep strict equality
_.isEqual([1, 2, 3], [1, 2, 3]); // true
_.isEqual([1, 2, 3], [1, 2]); // false
_.isEqual({ a: 'A', b: ['B'] }, { a: 'A', b: ['B'] }); // true
_.isEqual({ a: 'A', b: ['B'] }, { a: 'A', b: ['b'] }); // false
// Checks if a value is an empty enumerable object or non enumerable
_.isEmpty([]); // true
_.isEmpty([1]); // false
_.isEmpty({}); // true
_.isEmpty({ a: 'A' }); // false
_.isEmpty(''); // true
_.isEmpty('a'); // false
_.isEmpty('3'); // false
_.isEmpty(3); // true
_.isEmpty(undefined); // true - Because non enumerable object
_.isEmpty(null); // true
_.isEmpty(true); // true
// Gets a value from a object
var obj = { a: 1, b: { B: 2 }, c: [3, 4] };
_.get(obj, 'a'); // 1
_.get(obj, 'Z'); // undefined
_.get(obj, 'Z', 'defVal'); // 'defVal'
_.get(obj, 'b.B'); // 2
_.get(obj, ['b', 'B']); // 2
_.get(obj, 'c.1'); // 4
// Creates a unique ID
_.uuidv4(); // '9f1e53ba-3f08-4c9d-91c7-ad4226312f40'
// Creates a date string
_.createDateString(); // '20200528T065424+0900'
_.createDateString('yyyy-MM'); // '2020-05'
// Parses the date template literal to a date string.
_.parseDateLiteral('#{yyyy-MM-ddTHH:mm:ss}'); // '2020-01-02T15:04:05'
_.parseDateLiteral('#{yyyy/M/d H:m:s}'); // '2020/1/2 15:4:5'
_.parseDateLiteral('C:\\MyData\\#{yyyy-MM-dd}.txt'); // 'C:\MyData\2020-01-02.txt'
_.parseDateLiteral('\\\\MyNas\\#{yyyy}\\#{MM}\\#{dd}'); // '\\MyNas\2020\01\02'
_.parseDateLiteral('#{yyyy-[MM - 1]-dd}'); // '2019-12-02'
_.parseDateLiteral('#{yy[MM * 4]}'); // '2004'
// Converts a value to formatted string.
_.inspect(undefined); // 'undefined'
_.inspect(null); // 'null'
_.inspect(true); // 'true'
_.inspect(NaN); // 'NaN'
_.inspect('Foo'); // '"Foo"'
_.inspect(' '); // '" "'
_.inspect([1, NaN, '3']);
// '[
// 0: 1,
// 1: NaN,
// 2: "3",
// ]'
_.inspect({ a: [1, 2], b: true, o: { c: 'C' } });
// '{
// a: [
// 0: 1,
// 1: 2
// ],
// b: true,
// o: {
// c: "C"
// }
// }'
// Converts a schema object to a string
var schema = { comp: 'MYPC1234', share: 'C$', file: 'cache.db' };
_.parseTemplateLiteral('cp \\\\${comp}\\${share}\\${file} .\\tmp', schema);
// Returns: 'cp \\MYPC1234\C$\cache.db .\tmp'
// 半角カナを全角に変換
_.toZenkakuKana('もぅマヂ無理。'); // 'もぅマヂ無理'
// and so on...
Many other functions will be added. See the documentation for more details.
And you can also use all WshPolyfill functions. For example,
var array1 = [1, 4, 9, 16];
var map1 = array1.map(function(x) {
return x * 2;
});
console.dir(map1);
// Outputs: [2, 8, 18, 32]
var strJson = JSON.stringify({ from: array1, to: map1 });
console.log(strJson);
// Outputs: '{"from":[1,4,9,16],"to":[2,8,18,32]}'
// and so on...
You can also use the following helper functions in your JScript (.\MyScript.js).
See all specifications here and also below.
MIT
Copyright (c) 2020 Tuckn