diff --git a/dist/bundle.js b/dist/bundle.js deleted file mode 100644 index c2158a7..0000000 --- a/dist/bundle.js +++ /dev/null @@ -1 +0,0 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t=t||self).NeueKit={})}(this,(function(t){"use strict";var e="millisecond",n="second",r="minute",i="hour",o="day",u="week",s="month",a="quarter",c="year",f=/^(\d{4})-?(\d{1,2})-?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d{1,3})?$/,d=/\[([^\]]+)]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,h=function(t,e,n){var r=String(t);return!r||r.length>=e?t:""+Array(e+1-r.length).join(n)+t},l={s:h,z:function(t){var e=-t.utcOffset(),n=Math.abs(e),r=Math.floor(n/60),i=n%60;return(e<=0?"+":"-")+h(r,2,"0")+":"+h(i,2,"0")},m:function(t,e){var n=12*(e.year()-t.year())+(e.month()-t.month()),r=t.clone().add(n,s),i=e-r<0,o=t.clone().add(n+(i?-1:1),s);return Number(-(n+(e-r)/(i?r-o:o-r))||0)},a:function(t){return t<0?Math.ceil(t)||0:Math.floor(t)},p:function(t){return{M:s,y:c,w:u,d:o,D:"date",h:i,m:r,s:n,ms:e,Q:a}[t]||String(t||"").toLowerCase().replace(/s$/,"")},u:function(t){return void 0===t}},m={name:"en",weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_")},g="en",p={};p[g]=m;var $=function(t){return t instanceof M},v=function(t,e,n){var r;if(!t)return g;if("string"==typeof t)p[t]&&(r=t),e&&(p[t]=e,r=t);else{var i=t.name;p[i]=t,r=i}return!n&&r&&(g=r),r||!n&&g},y=function(t,e,n){if($(t))return t.clone();var r=e?"string"==typeof e?{format:e,pl:n}:e:{};return r.date=t,new M(r)},w=l;w.l=v,w.i=$,w.w=function(t,e){return y(t,{locale:e.$L,utc:e.$u,$offset:e.$offset})};var M=function(){function t(t){this.$L=this.$L||v(t.locale,null,!0),this.parse(t)}var h=t.prototype;return h.parse=function(t){this.$d=function(t){var e=t.date,n=t.utc;if(null===e)return new Date(NaN);if(w.u(e))return new Date;if(e instanceof Date)return new Date(e);if("string"==typeof e&&!/Z$/i.test(e)){var r=e.match(f);if(r)return n?new Date(Date.UTC(r[1],r[2]-1,r[3]||1,r[4]||0,r[5]||0,r[6]||0,r[7]||0)):new Date(r[1],r[2]-1,r[3]||1,r[4]||0,r[5]||0,r[6]||0,r[7]||0)}return new Date(e)}(t),this.init()},h.init=function(){var t=this.$d;this.$y=t.getFullYear(),this.$M=t.getMonth(),this.$D=t.getDate(),this.$W=t.getDay(),this.$H=t.getHours(),this.$m=t.getMinutes(),this.$s=t.getSeconds(),this.$ms=t.getMilliseconds()},h.$utils=function(){return w},h.isValid=function(){return!("Invalid Date"===this.$d.toString())},h.isSame=function(t,e){var n=y(t);return this.startOf(e)<=n&&n<=this.endOf(e)},h.isAfter=function(t,e){return y(t)1&&void 0!==c[1]?c[1]:{},r=c.length>2&&void 0!==c[2]&&c[2],i=c.length>3?c[3]:void 0,o=Object.keys(n).length?"?"+Y(n):"",u=e+o,t.next=7,r?r.fetch(u):fetch(u);case 7:return s=t.sent,i&&i(s),t.next=11,s.json();case 11:return a=t.sent,t.abrupt("return",a);case 13:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var C=Object.freeze({__proto__:null,lazy:function(t){if(process.browser&&x)return x.observe(t),{update:function(){x.unobserve(t),t.classList.remove("is-ready"),setTimeout((function(){return x.observe(t)}),200)},destroy:function(){x.unobserve(t)}}},pull:function(t){return k.apply(this,arguments)}});var I=Object.freeze({__proto__:null,copy:function(t){var e=document.createElement("textArea");e.setAttribute("type","hidden"),e.value=t,document.body.appendChild(e),e.focus(),e.select();try{document.execCommand("copy")}catch(t){console.error("Unable to copy:",t)}document.body.removeChild(e)},getIndex:function(t,e,n){return t.findIndex((function(t){return t[e]===n}))},getObject:function(t,e,n){return t.find((function(t){return t[e]===n}))},hex:function(){return Math.random().toString(36).substring(7)},htmlDecode:function(t){return t.replace(/&#(\d+);/g,(function(t,e){return String.fromCharCode(e)}))},leadZero:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return"".concat(t+parseInt("1".padEnd(2+e,"0"))).substring(1)},niceDate:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"DD MMMM YYYY";return _.extend(advancedFormat),_().format(e)},niceTime:function(t){return"".concat(t/60^0,"h ").concat(t%60,"m")},titleCase:function(t){return t=t.replace(/(^\w{1}|\.\s*\w{1})/gi,(function(t){return t.toUpperCase()}))}});var T=Object.freeze({__proto__:null,author:function(t){if(t.author)return t.author[0]},block:function(t){return t=(t||"").replace(/(lazyblock\/|core\/)/g,"").toLowerCase().replace(/(\b|-)\w/g,(function(t){return t.toUpperCase().replace(/-/,"")}))},categories:function(t){if(t._embedded&&t._embedded["wp:term"])return t._embedded["wp:term"][0]},categoryUrl:function(t,e){if(t)return t.link.replace(e,"")},mediaUrl:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"full";return!(!t._embedded||!t._embedded["wp:featuredmedia"]||t._embedded["wp:featuredmedia"][0].code)&&t._embedded["wp:featuredmedia"][0].media_details.sizes[e].source_url}});t.integrations=b,t.svelte=C,t.vanilla=I,t.wordpress=T,Object.defineProperty(t,"__esModule",{value:!0})})); diff --git a/docs/index.md b/docs/index.md index 3fd2213..89fa4c7 100644 --- a/docs/index.md +++ b/docs/index.md @@ -6,45 +6,50 @@ - [analytics][2] - [Parameters][3] - [dayjs][4] -- [dayjs][5] -- [Svelte][6] - - [lazy][7] - - [Parameters][8] - - [pull][9] + - [googlemaps][5] + - [Parameters][6] +- [dayjs][7] +- [Middleware][8] + - [sanslash][9] - [Parameters][10] - - [storable][11] - - [Parameters][12] -- [Vanilla][13] - - [copy][14] +- [Svelte][11] + - [lazy][12] + - [Parameters][13] + - [pull][14] - [Parameters][15] - - [getIndex][16] + - [storable][16] - [Parameters][17] - - [getObject][18] - - [Parameters][19] - - [hex][20] - - [htmlDecode][21] +- [Vanilla][18] + - [copy][19] + - [Parameters][20] + - [getIndex][21] - [Parameters][22] - - [leadZero][23] + - [getObject][23] - [Parameters][24] - - [netCheck][25] - - [Parameters][26] - - [niceDate][27] - - [Parameters][28] - - [niceTime][29] - - [Parameters][30] - - [titleCase][31] - - [Parameters][32] -- [WordPress][33] - - [author][34] + - [hex][25] + - [htmlDecode][26] + - [Parameters][27] + - [leadZero][28] + - [Parameters][29] + - [netCheck][30] + - [Parameters][31] + - [niceDate][32] + - [Parameters][33] + - [niceTime][34] - [Parameters][35] - - [block][36] + - [titleCase][36] - [Parameters][37] - - [categories][38] - - [Parameters][39] - - [categoryUrl][40] - - [Parameters][41] - - [mediaUrl][42] - - [Parameters][43] +- [WordPress][38] + - [author][39] + - [Parameters][40] + - [block][41] + - [Parameters][42] + - [categories][43] + - [Parameters][44] + - [categoryUrl][45] + - [Parameters][46] + - [mediaUrl][47] + - [Parameters][48] ## Integrations @@ -56,7 +61,7 @@ Initialises a minimal Google Analytics script to track traffic #### Parameters -- `gaID` **[string][44]** the Google Analytics id eg. UA-123456789-1 +- `gaID` **[string][49]** the Google Analytics id eg. UA-123456789-1 **Meta** @@ -66,7 +71,19 @@ Initialises a minimal Google Analytics script to track traffic Initialises the dayjs plugin, with advancedFormat enabled -Returns **[object][45]** dayjs instance +Returns **[object][50]** dayjs instance + +**Meta** + +- **version**: 1.0.0 + +### googlemaps + +Allows for programmatic insertion of Google Maps script + +#### Parameters + +- `options` **[object][50]** the Google Maps Api params (optional, default `{}`) **Meta** @@ -77,6 +94,24 @@ Returns **[object][45]** dayjs instance Wrapped in callback function to avoid invoking when read/imported e.g. export default dayjs.extend(advancedFormat); <- Is not tree shakeable +## Middleware + +Integration modules + +### sanslash + +Redirects all urls ending in a trailing slash to non trailing for SEO + +#### Parameters + +- `req` **[object][50]** request object +- `res` **[object][50]** response object +- `next` **[function][51]** next step function + +**Meta** + +- **version**: 1.0.0 + ## Svelte Svelte modules @@ -87,9 +122,9 @@ Constructs an IntersectionObserver based, lazy load function for svelte #### Parameters -- `node` **[object][45]** the element to be observed/lazy loaded +- `node` **[object][50]** the element to be observed/lazy loaded -Returns **[object][45]** containing svelte action methods, update and destroy +Returns **[object][50]** containing svelte action methods, update and destroy **Meta** @@ -101,12 +136,12 @@ Handy sapper fetch wrapper #### Parameters -- `endpoint` **[string][44]** api url -- `params` **[object][45]** object to convert to query string (optional, default `{}`) -- `preload` **[boolean][46]** instruction to use sappers preload fetch (optional, default `false`) -- `callback` **[function][47]** a callback function to be run post fetch +- `endpoint` **[string][49]** api url +- `params` **[object][50]** object to convert to query string (optional, default `{}`) +- `preload` **[boolean][52]** instruction to use sappers preload fetch (optional, default `false`) +- `callback` **[function][51]** a callback function to be run post fetch -Returns **[object][45]** fetch data in json format +Returns **[object][50]** fetch data in json format **Meta** @@ -120,7 +155,7 @@ Svelte store which reads/writes values to the users localStorage - `data` **any** data to create store with -Returns **[object][45]** store methods +Returns **[object][50]** store methods **Meta** @@ -136,7 +171,7 @@ Copies text to the users' clipboard #### Parameters -- `text` **[string][44]** the string to be copied +- `text` **[string][49]** the string to be copied **Meta** @@ -148,11 +183,11 @@ Finds an object index in an array of objects #### Parameters -- `arr` **[array][48]** the array of objects to search through -- `key` **[string][44]** the property key in the object you want to find -- `value` **[string][44]** the property value in the object you want to find +- `arr` **[array][53]** the array of objects to search through +- `key` **[string][49]** the property key in the object you want to find +- `value` **[string][49]** the property value in the object you want to find -Returns **[number][49]** index of the object that matches the criteria, -1 otherwise +Returns **[number][54]** index of the object that matches the criteria, -1 otherwise **Meta** @@ -164,11 +199,11 @@ Finds an object in an array of objects #### Parameters -- `arr` **[array][48]** the array of objects to search through -- `key` **[string][44]** the property key in the object you want to find -- `value` **[string][44]** the property value in the object you want to find +- `arr` **[array][53]** the array of objects to search through +- `key` **[string][49]** the property key in the object you want to find +- `value` **[string][49]** the property value in the object you want to find -Returns **[number][49]** the object that matches the criteria, undefined otherwise +Returns **[number][54]** the object that matches the criteria, undefined otherwise **Meta** @@ -178,7 +213,7 @@ Returns **[number][49]** the object that matches the criteria, undefined otherwi Creates a random 6 character hex value -Returns **[string][44]** 6 character hex string +Returns **[string][49]** 6 character hex string **Meta** @@ -190,9 +225,9 @@ Decodes a numeric html entity such as J #### Parameters -- `str` **[string][44]** the string to be decoded +- `str` **[string][49]** the string to be decoded -Returns **[string][44]** plain text string +Returns **[string][49]** plain text string **Meta** @@ -204,10 +239,10 @@ Adds leading zeros to your number #### Parameters -- `number` **[number][49]** The number to prefix with zeros -- `zeros` **[number][49]** The number of zeros to lead by (optional, default `0`) +- `number` **[number][54]** The number to prefix with zeros +- `zeros` **[number][54]** The number of zeros to lead by (optional, default `0`) -Returns **[string][44]** 6 character hex string +Returns **[string][49]** 6 character hex string **Meta** @@ -221,7 +256,7 @@ Does a rudimentary check of download speed - `size` (optional, default `100000`) -Returns **[number][49]** Filesize in bytes +Returns **[number][54]** Filesize in bytes **Meta** @@ -233,10 +268,10 @@ Creates a nice formatted date from a unix timestamp #### Parameters -- `time` **[number][49]** unix timestamp -- `format` **[string][44]** dayjs format (optional, default `'DD MMMM YYYY'`) +- `time` **[number][54]** unix timestamp +- `format` **[string][49]** dayjs format (optional, default `'DD MMMM YYYY'`) -Returns **[string][44]** 6 character hex string +Returns **[string][49]** 6 character hex string **Meta** @@ -248,9 +283,9 @@ Creates a nice formatted time from n integer of minutes #### Parameters -- `mins` **[number][49]** minutes integer +- `mins` **[number][54]** minutes integer -Returns **[string][44]** nicely formatted time string eg. 7h 14m +Returns **[string][49]** nicely formatted time string eg. 7h 14m **Meta** @@ -262,9 +297,9 @@ Takes a string and converts it to Title case #### Parameters -- `str` **[string][44]** string to convert +- `str` **[string][49]** string to convert -Returns **[string][44]** converted string +Returns **[string][49]** converted string **Meta** @@ -280,9 +315,9 @@ Shortcut to the author object #### Parameters -- `data` **[object][45]** the post object +- `data` **[object][50]** the post object -Returns **[object][45]** the author of the post +Returns **[object][50]** the author of the post **Meta** @@ -294,9 +329,9 @@ Returns a component name based on a lazyblock slug #### Parameters -- `block` **[object][45]** the block object +- `block` **[object][50]** the block object -Returns **[string][44]** the component name for use in <svelte:component> +Returns **[string][49]** the component name for use in <svelte:component> **Meta** @@ -308,9 +343,9 @@ Shortcut to wp:term #### Parameters -- `data` **[object][45]** the post/page object +- `data` **[object][50]** the post/page object -Returns **[object][45]** term object +Returns **[object][50]** term object **Meta** @@ -322,10 +357,10 @@ Gets relative pathname for taxonomy url #### Parameters -- `data` **[object][45]** the post/page object -- `url` **[string][44]** the site domain name, including protocol +- `data` **[object][50]** the post/page object +- `url` **[string][49]** the site domain name, including protocol -Returns **[string][44]** the relative category url +Returns **[string][49]** the relative category url **Meta** @@ -337,10 +372,10 @@ Shortcut to wp:featuredmedia #### Parameters -- `data` **[object][45]** the post/page object -- `size` **[string][44]** the wp image size (optional, default `'full'`) +- `data` **[object][50]** the post/page object +- `size` **[string][49]** the wp image size (optional, default `'full'`) -Returns **[string][44]** featured image url +Returns **[string][49]** featured image url **Meta** @@ -354,92 +389,102 @@ Returns **[string][44]** featured image url [4]: #dayjs -[5]: #dayjs-1 +[5]: #googlemaps -[6]: #svelte +[6]: #parameters-1 -[7]: #lazy +[7]: #dayjs-1 -[8]: #parameters-1 +[8]: #middleware -[9]: #pull +[9]: #sanslash [10]: #parameters-2 -[11]: #storable +[11]: #svelte -[12]: #parameters-3 +[12]: #lazy -[13]: #vanilla +[13]: #parameters-3 -[14]: #copy +[14]: #pull [15]: #parameters-4 -[16]: #getindex +[16]: #storable [17]: #parameters-5 -[18]: #getobject +[18]: #vanilla -[19]: #parameters-6 +[19]: #copy -[20]: #hex +[20]: #parameters-6 -[21]: #htmldecode +[21]: #getindex [22]: #parameters-7 -[23]: #leadzero +[23]: #getobject [24]: #parameters-8 -[25]: #netcheck +[25]: #hex -[26]: #parameters-9 +[26]: #htmldecode -[27]: #nicedate +[27]: #parameters-9 -[28]: #parameters-10 +[28]: #leadzero -[29]: #nicetime +[29]: #parameters-10 -[30]: #parameters-11 +[30]: #netcheck -[31]: #titlecase +[31]: #parameters-11 -[32]: #parameters-12 +[32]: #nicedate -[33]: #wordpress +[33]: #parameters-12 -[34]: #author +[34]: #nicetime [35]: #parameters-13 -[36]: #block +[36]: #titlecase [37]: #parameters-14 -[38]: #categories +[38]: #wordpress + +[39]: #author + +[40]: #parameters-15 + +[41]: #block + +[42]: #parameters-16 + +[43]: #categories -[39]: #parameters-15 +[44]: #parameters-17 -[40]: #categoryurl +[45]: #categoryurl -[41]: #parameters-16 +[46]: #parameters-18 -[42]: #mediaurl +[47]: #mediaurl -[43]: #parameters-17 +[48]: #parameters-19 -[44]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String +[49]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String -[45]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object +[50]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object -[46]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean +[51]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function -[47]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function +[52]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean -[48]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array +[53]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array -[49]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number +[54]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number diff --git a/package.json b/package.json index 6cf62c3..21a03ea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@neuekit/utilities", - "version": "1.1.1", + "version": "1.2.0", "license": "MIT", "description": "Tree-shakeable JavaScript utilities, add yours today!", "main": "dist/bundle.js", diff --git a/rollup.config.js b/rollup.config.js index e8dce69..11a119e 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -25,22 +25,15 @@ const plugins = [ ]; export default [ - { - input: './src/index.js', - output: { - file: './dist/bundle.js', - format: 'umd', - name: 'NeueKit' - }, - plugins - }, { input: './testing/index.js', output: { file: './testing/bundle.js', format: 'iife', - name: 'NeueKit' + name: 'NeueKit', + globals: { 'svelte/store': 'sveltestore' } }, - plugins + plugins, + external: [ 'svelte/store' ] } ]; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 90a8c7f..fe351dc 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,12 @@ import * as integrations from './integrations'; +import * as middleware from './middleware'; import * as svelte from './svelte'; import * as vanilla from './vanilla'; import * as wordpress from './wordpress'; export { integrations, + middleware, svelte, vanilla, wordpress diff --git a/src/integrations/googlemaps.js b/src/integrations/googlemaps.js new file mode 100644 index 0000000..12bc2a6 --- /dev/null +++ b/src/integrations/googlemaps.js @@ -0,0 +1,33 @@ +/** + * @private + */ +let googleMapsScriptIsInjected = false; + +/** + * Allows for programmatic insertion of Google Maps script + * @memberof Integrations + * @version 1.0.0 + * @param {object} options the Google Maps Api params + */ +export default function(options = {}) { + if (googleMapsScriptIsInjected) { + window[options.callback](); + return; + } + + const optionsQuery = Object.keys(options) + .map(k => `${encodeURIComponent(k)}=${encodeURIComponent(options[k])}`) + .join('&'); + + const url = `https://maps.googleapis.com/maps/api/js?${optionsQuery}`; + + const script = document.createElement('script'); + + script.setAttribute('src', url); + script.setAttribute('async', ''); + script.setAttribute('defer', ''); + + document.head.appendChild(script); + + googleMapsScriptIsInjected = true; +} diff --git a/src/integrations/index.js b/src/integrations/index.js index ff2ed71..c074436 100644 --- a/src/integrations/index.js +++ b/src/integrations/index.js @@ -1,9 +1,10 @@ import analytics from './analytics'; import dayjs from './dayjs'; +import googlemaps from './googlemaps'; /** * Integration modules * @namespace Integrations */ -export { analytics, dayjs }; +export { analytics, dayjs, googlemaps }; diff --git a/src/middleware/index.js b/src/middleware/index.js new file mode 100644 index 0000000..474b834 --- /dev/null +++ b/src/middleware/index.js @@ -0,0 +1,8 @@ +import sanslash from './sanslash'; + +/** + * Integration modules + * @namespace Middleware + */ + +export { sanslash }; diff --git a/src/middleware/sanslash.js b/src/middleware/sanslash.js new file mode 100644 index 0000000..15c8c1c --- /dev/null +++ b/src/middleware/sanslash.js @@ -0,0 +1,18 @@ +/** + * Redirects all urls ending in a trailing slash to non trailing for SEO + * @memberof Middleware + * @version 1.0.0 + * @param {object} req request object + * @param {object} res response object + * @param {function} next next step function + */ +export default function(req, res, next) { + if (req.path.substr(-1) == '/' && req.path.length > 1) { + const query = req.url.slice(req.path.length); + res.statusCode = 301; + res.setHeader('Location', req.path.slice(0, -1) + query); + res.end(); + } else { + next(); + } +}