xtejs-utils
This is a general utility class and function package.
Installation
npm install xtejs-utils;
Usage
Browser detector.
; const browser = navigatoruserAgent; // All browser parsed resultsbrowser;// {// "browser": {// "name": "Chrome",// "version": "80.0.3987.163"// },// "os": {// "name": "Android",// "version": "8.0",// "versionName": "Oreo"// },// "platform": {// "type": "mobile"// },// "engine": {// "name": "Blink"// }// } // Browser's namebrowser;// Chrome // Browser's versionbrowser;// 80.0.3987.163 // OS namebrowser;// Android // OS versionbrowser;// 8.0 // Platform namebrowser;// mobile // Engines's namebrowser;// Blink
Collection utility.
; // Removes duplicate values from an arrayconst input = 'green' 'red''green' 'blue' 'red';Collection;// [// "green",// "red",// "blue"// ]
System Colors.
The following color values are various colors that adapt to changing accessibility settings.
; Colorblue;// rgb(0,122,255)ColordarkBlue;// rgb(10,132,255)ColoraccessibleBlue;// rgb(0,64,221)ColoraccessibleDarkBlue;// rgb(64,156,255)
Date utility.
; // Get the current date and time in any formatDatetime;// 2020-03-20T12:17:34+09:00Datetime;// Friday, March 20th 2020, 12:17:34 pmDatetime;// Fri, 12PM // Get any date and time in any formatDatetime;// 2020-01-01T00:00:00+09:00Datetime;// Wednesday, January 1st 2020, 12:00:00 amDatetime;// Wed, 12AM // Get time from 00:00Datetime;// ["00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00","08:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00","00:00"] // Get the time from 09:00Datetime;// ["09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00","17:00","18:00","19:00","20:00","21:00","22:00","23:00","00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00","08:00","09:00"] // Get the time from 00:00 in any formatDatetime;// ["12:00 AM","1:00 AM","2:00 AM","3:00 AM","4:00 AM","5:00 AM","6:00 AM","7:00 AM","8:00 AM","9:00 AM","10:00 AM","11:00 AM","12:00 PM","1:00 PM","2:00 PM","3:00 PM","4:00 PM","5:00 PM","6:00 PM","7:00 PM","8:00 PM","9:00 PM","10:00 PM","11:00 PM","12:00 AM"] // Get the time from 09:00 in any formatDatetime;// ["9:00 AM","10:00 AM","11:00 AM","12:00 PM","1:00 PM","2:00 PM","3:00 PM","4:00 PM","5:00 PM","6:00 PM","7:00 PM","8:00 PM","9:00 PM","10:00 PM","11:00 PM","12:00 AM","1:00 AM","2:00 AM","3:00 AM","4:00 AM","5:00 AM","6:00 AM","7:00 AM","8:00 AM","9:00 AM"] // Get the day of the current month in any formatDatetime;// ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"] // Get the day of the month in any formatDatetime;// [["Mar 1","Mar 2","Mar 3","Mar 4","Mar 5","Mar 6","Mar 7","Mar 8","Mar 9","Mar 10","Mar 11","Mar 12","Mar 13","Mar 14","Mar 15","Mar 16","Mar 17","Mar 18","Mar 19","Mar 20","Mar 21","Mar 22","Mar 23","Mar 24","Mar 25","Mar 26","Mar 27","Mar 28","Mar 29","Mar 30","Mar 31"] // Get day of any monthDatetime;// ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"] // Get day of any month in any formatDatetime;"Jan 1""Jan 2""Jan 3""Jan 4""Jan 5""Jan 6""Jan 7""Jan 8""Jan 9""Jan 10""Jan 11""Jan 12""Jan 13""Jan 14""Jan 15""Jan 16""Jan 17""Jan 18""Jan 19""Jan 20""Jan 21""Jan 22""Jan 23""Jan 24""Jan 25""Jan 26""Jan 27""Jan 28""Jan 29""Jan 30""Jan 31" // Get date every 7 years from 2020 to 2030Datetime;// ["2020","2027"] // Get date every 7 months from January 2020 to January 2030Datetime;// ["1/2020","8/2020","3/2021","10/2021","5/2022","12/2022","7/2023","2/2024","9/2024","4/2025","11/2025","6/2026","1/2027","8/2027","3/2028","10/2028","5/2029","12/2029"] // Get date every 1 week from March 1 to March 31Datetime;// ["Thu, 3/1","Thu, 3/8","Thu, 3/15","Thu, 3/22","Thu, 3/29"] // Get date every 7 days from March 1 to March 31Datetime;// ["3/1","3/8","3/15","3/22","3/29"] // Get time every 7 hours from 0:00, March 1 to 0:00, March 2Datetime;// ["0:00","7:00","14:00","21:00"] // Get time every 30 minutes from 9:00 to 12:00Datetime;// ["9:00","9:30","10:00","10:30","11:00","11:30","12:00"] // Get time every 30 seconds from 9:00 to 9:03Datetime;// ["9:00:00","9:00:30","9:01:00","9:01:30","9:02:00","9:02:30","9:03:00","9:03:30","9:04:00","9:04:30","9:05:00"] // Add yearsDatetime;// 3/21/2021 // Add monthsDatetime;// 4/21/2020 // Add weeksDatetime;// 3/28/2020 // Add daysDatetime;// 3/22/2020 // Add hoursDatetime;// 3/21/2020, 10:00:00 // Add minutesDatetime;// 3/21/2020, 9:01:00 // Add secondsDatetime;// 3/21/2020, 9:00:01 // Check if date is invalidDatetime; // trueDatetime; // falseDatetime; // falseDatetime; // false (not a real month)Datetime; // false (not a real day)Datetime; // false (not a leap year)Datetime; // false (not a real month name)
Graphic utility.
This will be explained later
Miscellaneous utility.
; // Return whether Node.js environmentMisc;// Returns false if the browser, true if Node.js // Returns a window object for browsers and a global object for Node.jsMisc;
Random utility.
; // Returns a number from 3 to 9Random; // Get 1 element randomly from 3 elementsRandom; // Basic pure ROYGBIV colors (sometimes)Random; // Very dark colors (Rare)Random; // Light dreamy pastels (very frequent)Random;
Template engine.
A template language that can generate HTML markup with JS.
Compiles a template so it can be executed immediately.
; // Compiles a template so it can be executed immediately. // #Basic Usagelet template = Template; ;// <p>Hello, my name is Alan.I am from Texas.I have 2 kids:</p>// <ul>// <li>Jimmy is 12</li>// <li>Sally is 4</li>// </ul> ;// <p>Hello, my name is Softly.I am from Michigan.I have 2 kids:</p>// <ul>// <li>Potter is 9</li>// <li>Ludge is 7</li>// </ul> // #iftemplate = Template; ;// <h1>Yehuda Katz</h1> ;// <h1>Unknown Author</h1> // #HTML-escapingTemplate specialChars: "& < > \" ' ` =" ;// raw: & < > " ' ` =// html-escaped: & < > " ' ` = // #eachTemplate people: 'Yehuda Katz' 'Alan Johnson' 'Charles Jolley' ;// <ul class="people_list">// <li>Yehuda Katz</li>// <li>Alan Johnson</li>// <li>Charles Jolley</li>// </ul> Template persons: name: 'Nils' country: 'Germany' name: 'Yehuda' country: 'USA' ;// <ul>// <li>Nils (Germany)</li>// <li>Yehuda (USA)</li>// </ul> // Round the decimal pointTemplate value: 14 ;// 1.4 round to 1Template value: 15 ;// 1.5 round to 2Template value: 16 ;// 1.6 round to 2 // Rounds the second decimal placeTemplate value: 1016 ;// 1.016 round to 1.02Template value: 1015 ;// 1.015 round to 1.02Template value: 1014 ;// 1.014 round to 1.01 // Round up the decimal pointTemplate value: 14 ;// 1.4 ceil to 2Template value: 15 ;// 1.5 ceil to 2Template value: 16 ;// 1.6 ceil to 2 // Rounded up to two decimal placesTemplate value: 1016 ;// 1.016 ceil to 1.02Template value: 1015 ;// 1.015 ceil to 1.02Template value: 1014 ;// 1.014 ceil to 1.02 // Round down the decimal pointTemplate value: 14 ;// 1.4 floor to 1Template value: 15 ;// 1.5 floor to 1Template value: 16 ;// 1.6 floor to 1 // Rounded down to two decimal placesTemplate value: 1016 ;// 1.016 floor to 1.01Template value: 1015 ;// 1.015 floor to 1.01Template value: 1014 ;// 1.014 floor to 1.01
Returns the result of compiling the template
; // #Basic UsageTemplate;// <p>Hello, my name is Beil.I am from New York.I have 1 kids:</p>// <ul>// <li>Jollye is 20</li>// </ul> // #ifTemplate;// <h1>Yehuda Katz</h1> Template;// <h1>Unknown Author</h1> // #HTML-escapingTemplate;// raw: & < > " ' ` =// html-escaped: & < > " ' ` = // #eachTemplate;// <ul class="people_list">// <li>Yehuda Katz</li>// <li>Alan Johnson</li>// <li>Charles Jolley</li>// </ul> Template;// <ul>// <li>Nils (Germany)</li>// <li>Yehuda (USA)</li>// </ul> // Round the decimal pointTemplate;// 1.4 round to 1Template;// 1.5 round to 2Template;// 1.6 round to 2 // Rounds the second decimal placeTemplate;// 1.016 round to 1.02Template;// 1.015 round to 1.02Template;// 1.014 round to 1.01 // Round up the decimal pointTemplate;// 1.4 ceil to 2Template;// 1.5 ceil to 2Template;// 1.6 ceil to 2 // Rounded up to two decimal placesTemplate;// 1.016 ceil to 1.02Template;// 1.015 ceil to 1.02Template;// 1.014 ceil to 1.02 // Round down the decimal pointTemplate;// 1.4 floor to 1Template;// 1.5 floor to 1Template;// 1.6 floor to 1 // Rounded down to two decimal placesTemplate;// 1.016 floor to 1.01Template;// 1.015 floor to 1.01Template;// 1.014 floor to 1.01
Type utility.
Utility for type determination and type related processing.
; // Returns whether it is an Async function {} Types;// falseTypes;// true
Examples
There are some examples in "./examples" in this package.Here is the first one to get you started.