javascript
How do I add types to a Vite library build?
You can use vite-plugin-dts import dts from “vite-plugin-dts”; export default defineConfig({ plugins: [ dts({ insertTypesEntry: true, }), ],
How to use render function in Vue3
try it. <script lang=”tsx” setup> import { h } from ‘vue’; const render = () => { return h(‘div’, []); }; const jsxNode = () => { return <div> text </div>; }; </script> <template> <render /> <jsxNode /> </template>
How to use XHR API in Node.js?
You don’t really need an XHR, since you can use http.request that comes natively with NodeJS, with it you can send GET, POST and PUT requests with headers and body. Here is the link to the documentation http.request.
Merge two objects but only existing properties
Assuming you only want enumerable properties, this is easily done with Object.keys and in (or hasOwnProperty): Object.keys(obj2).forEach(function(key) { if (key in obj1) { // or obj1.hasOwnProperty(key) obj1[key] = obj2[key]; } }); Example: var obj1 = { “name”: “”, “age”: “” }; var obj2 = { “name”: “Leo”, “age”: “14”, “company”: “aero”, “shift”: “night” }; Object.keys(obj2).forEach(function(key) … Read more
Unable to use structuredClone() on value of ref variable
The error means that structuredClone was executed on Proxy instance, which cannot be cloned. In order to allow this, it should be used on raw object that a proxy wraps: const b = structuredClone(toRaw(a.value)); Notice that toRaw is used on a.value because both a and a.value are reactive objects, and toRaw works shallowly and needs … Read more
Vue 3 – “Failed to resolve component” with global components
Registering components in the root component’s components option doesn’t make them global. Doing that just makes them available to the root component itself, not its children. To register components globally, use app.component in your top-level code: main.js import { createApp } from ‘vue’; import App from ‘./App.vue’; import MyGlobalComponent from ‘./components/MyGlobalComponent.vue’; const app = createApp(App); … Read more
ES5 Object.assign equivalent
In underscore.js you can use like, _.extend(firstObj, secondObj); In jQuery, you can use, $.extend({},firstObj,secondObj); In pure javascipt, you can merge n number of objects by using this function: function mergeObjects() { var resObj = {}; for(var i=0; i < arguments.length; i += 1) { var obj = arguments[i], keys = Object.keys(obj); for(var j=0; j < … Read more
Javascript detect scrollbar in textarea
function has_scrollbar(elem_id) { const elem = document.getElementById(elem_id); if (elem.clientHeight < elem.scrollHeight) alert(“The element has a vertical scrollbar!”); else alert(“The element doesn’t have a vertical scrollbar.”); } See this jsFiddle http://jsfiddle.net/qKNXH/
How to pixelate an image with canvas and javascript
You don’t need to iterate pixel buffer to create a pixelating effect. Simply turn off image smoothing and enlarge a small version of the image to the canvas. This will also mean you can use any image as source (CORS-wise). Example: Fiddle demo // get a block size (see demo for this approach) var size … Read more