|
necrotic posted:One of the biggest lessons to learn from that help is to work at breaking down what you are doing into smaller chunks. One big function doing a lot of stuff is generally a signal that it can be broken down into smaller "units of work" that each individually solve a smaller part of the larger problem. If you have a three-line *condition*, on the other hand, that's something that is probably worth making a little function for, because if nothing else if (thing.smellsBad()) is much easier to quickly understand in passing when you're reading your code later than if (thing.scentRatio>0.8 && thing.scentType==ScentType.Bad && !dog.hasNoNose).
|
# ? Feb 24, 2024 01:46 |
|
|
# ? Jun 5, 2024 05:26 |
|
One case where I will make completely trivial functions is when I'm filtering a collection. So if you have multple things in a list then instead ofJavaScript code:
JavaScript code:
|
# ? Feb 24, 2024 17:55 |
|
I would split into two filter calls with anonymous functions if it was a one off. If those specific filtering rules apply to multiple spots then yeah I’d define helper functions. For a one-off I’d argue that’s “too much”
|
# ? Feb 24, 2024 18:49 |
|
This feels like a really stupid question but I cant figure out what the transom I have to bridge is: I have a webpack that has sortablejs in it, and I'd like to be able to call sortablejs from the browser's dev tools console, just like i could when I was using jquery, so I can interactively play around with it. how do I do that? my full tech stack is rails and yarn based
|
# ? Feb 24, 2024 19:04 |
|
KoRMaK posted:This feels like a really stupid question but I cant figure out what the transom I have to bridge is: I have a webpack that has sortablejs in it, and I'd like to be able to call sortablejs from the browser's dev tools console, just like i could when I was using jquery, so I can interactively play around with it. how do I do that? my full tech stack is rails and yarn based If you’ve webpacked everything is in one module with a bespoke “require/import” to work in that one bundle. The hacky stupid approach I will suggest is to do a window.sortablejs = require (…) somewhere in your sources to make the package available to the console. There may be a better solution, I don’t know or care to look because bundling generally works this way. import in web (as you are using in the console) is very specific to ES modules. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules If your webpack can / is setup to spit out ESM that would work with import it may be changing the module name, but I haven’t really worked with ESM so don’t really know. Edit: and you are seeing the original source with the webpack:// prefixes because of Source Maps. That is _not_ the actual JS that is executing in the browser, it’s mapping that browser file back to the original sources for debugging. necrotic fucked around with this message at 19:19 on Feb 24, 2024 |
# ? Feb 24, 2024 19:14 |
|
necrotic posted:
code:
|
# ? Feb 24, 2024 19:34 |
|
Alternative method: set a breakpoint in your JS code in a place where where that package is available. When the debugger hits the breakpoint and JS execution is paused, your console will have access to all variables in scope for the currently executing code.
|
# ? Feb 25, 2024 16:21 |
|
Ima Computer posted:Alternative method: set a breakpoint in your JS code in a place where where that package is available. When the debugger hits the breakpoint and JS execution is paused, your console will have access to all variables in scope for the currently executing code.
|
# ? Feb 25, 2024 16:44 |
|
KoRMaK posted:Yes I had considered this but since you are at a breakpoint time is frozen and you can't interact with the rendered browser ui as you normally would. You should be able to assign whatever you want to a variable (possibly on the window again) and then resume with that approach.
|
# ? Feb 25, 2024 17:41 |
|
that's a very neat trick and has managed to dispell a lot of mystery around using node modules in my project that i just hadn't given a poo poo to dive into. now i understand how to grow another piece of my infra. soooo long inline javascript
|
# ? Feb 25, 2024 18:43 |
|
I was playing around with making a calendar, and I'm very confused about getDay(). I know 0 = Sun, it's not that. Why is it putting in an ISO string makes the value 1 less than it should be? JavaScript code:
|
# ? Mar 19, 2024 18:24 |
|
lol, use Luxon DateTime for anything more than fetching the current time. REF: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date quote:Note: While the time value at the heart of a Date object is UTC, the basic methods to fetch the date and time or its components all work in the local (i.e. host system) time zone and offset. The long form date is in the local timezone, the short form date is in UTC. NodeJS CLI is good for debugging this. code:
|
# ? Mar 19, 2024 18:45 |
|
MrMoo posted:lol, use Luxon DateTime for anything more than fetching the current time. So because a time zone isn't defined, it's using a time zone where September 11, 2001 was a Monday? OK
|
# ? Mar 19, 2024 19:38 |
|
The Merkinman posted:So because a time zone isn't defined, it's using a time zone where September 11, 2001 was a Monday? OK No, it's because you are calling the function from a time zone where it was still monday september 10th in UTC time. As another poster said, don't use JS Date object to parse a date. It's better to use something like moment or whatever the new hotness is.
|
# ? Mar 19, 2024 19:56 |
|
go play outside Skyler posted:As another poster said, don't use JS Date object to parse a date. It's better to use something like moment or whatever the new hotness is. don't use moment in new code. https://momentjs.com/docs/#/-project-status/ it's funny because there were all these years of "don't use raw dates, use moment", and now it's luxon. oh well.
|
# ? Mar 19, 2024 20:31 |
|
https://dev.to/zachgoll/a-complete-guide-to-javascript-dates-and-why-your-date-is-off-by-1-day-fi1 I believe this article covers it. And yeah don't use javascript date objects, use some library like moment.js edit: nm don't use moment
|
# ? Mar 19, 2024 20:36 |
|
Does anyone know why Temporal is still not ready?
|
# ? Mar 19, 2024 20:48 |
|
Death by committee ?
|
# ? Mar 19, 2024 20:51 |
|
Obfuscation posted:Does anyone know why Temporal is still not ready? Blame Microsoft for wanting a 1601 epoch and 100ns units? It has to be something esoteric, probably more related to the multitude of complex calendaring schemes. There are two polyfills though: https://github.com/js-temporal/temporal-polyfill#readme https://github.com/fullcalendar/temporal-polyfill#readme I was impressed a lot of nice stuff has been hitting V8 recently: https://v8.dev/blog/holiday-season-2023#:~:text=New%20JavaScript%20features MrMoo fucked around with this message at 20:57 on Mar 19, 2024 |
# ? Mar 19, 2024 20:52 |
|
Bruegels Fuckbooks posted:don't use moment in new code. https://momentjs.com/docs/#/-project-status/ I like date-fns myself!
|
# ? Mar 19, 2024 23:19 |
|
Big Day.js fan here
|
# ? Mar 20, 2024 02:22 |
|
would using the regular Date lib with only UTC avoid these kinds of mixups? that's what we do at my job.
|
# ? Mar 20, 2024 03:11 |
|
Doktor Avalanche posted:would using the regular Date lib with only UTC avoid these kinds of mixups? that's what we do at my job. In general yes... until someone does new Date('September 11, 2001') with the crazy expectation that it will act the same as new Date('2001-09-11'). The standard Javascript Date object is fundamentally flawed. The only advisable way to use it is delete globalThis.Date.
|
# ? Mar 20, 2024 07:20 |
|
biznatchio posted:In general yes... until someone does new Date('September 11, 2001') with the crazy expectation that it will act the same as new Date('2001-09-11'). Date.parse in particular is hella hosed up. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse#examples Some highlights: 1. Non standard date strings were allowed to be implementation specific, so there's no guarantee they work the same between any browser. 2. There's no parameter for the expected format of the date string, or region of the date string, so it's up to the browser implementation to guess from the content of the string which date format it is - which is great when it tries to parse a string like 9-11-2001, which could easily parse as December 9, 2001 in some parts of the world.
|
# ? Mar 20, 2024 15:11 |
|
Writing my own SQL ORM, I have a schema like this:JavaScript code:
JavaScript code:
I've played around with putting the generated types in the project directory but I'm having a lot of problems when it comes to using those types inside my module, I'm effectively forced to use dynamic imports which are asynchronous. I asked copilot and it says I can do funny things with typescript like this: JavaScript code:
Nolgthorn fucked around with this message at 02:28 on Mar 21, 2024 |
# ? Mar 21, 2024 01:53 |
|
I don't know but many validators (eg Zod) have code schema -> TS without generating type files so for simple cases it seems possible to avoid it. Do you really need to generate type files?
|
# ? Mar 21, 2024 02:10 |
|
It would be really cool if I could figure out how not to. One of the problems with generating the types file is that my orm can only really support one database at a time without risking types mapping over one another.
|
# ? Mar 21, 2024 02:29 |
|
Having trouble getting anything else to work but I found a solution to the "only one database" problem. Have schema include a type prefix:JavaScript code:
JavaScript code:
JavaScript code:
It's sort of like I definitely have to use generics, I can't just lookup types using strings in my code... generics is how I did it originally but I thought I'd try and find a way to remove the litany of generics everywhere in the codebase. Nolgthorn fucked around with this message at 13:30 on Mar 21, 2024 |
# ? Mar 21, 2024 12:58 |
|
Perhaps in an effort to go crazy I have made a little bit of headway regarding inferred types.JavaScript code:
However this is about as far as I can go. I am using `createSchema(mySchema)` to provide the user with types while building the object. There isn't any way at least that I've found to have `createSchema` accept an object of a specific type and also have that object be of type const. JavaScript code:
So that's a roadblock. The other big setback is that I still need my Address type to have a user parameter that maps to the User type. Also visa-versa. In code I'm figuring out what these associations are with a sweet javascript function that calculates all of them. But I can't use that... because then I'm no longer in type world. It's like I have to look at the Address table referencing the User table and then that means the User type should have a address parameter. Almost like instead of having the user define foreign keys, I should be doing that programmatically and instead have them define what all the relationships between tables are. A massive change largely just so that I can more easily negotiate types. Furthermore I'm really not certain what happens if the Address object type infers a User object type which infers a Address object type. Isn't that gonna spin my cpus into the stratosphere? Nolgthorn fucked around with this message at 11:27 on Mar 26, 2024 |
# ? Mar 25, 2024 22:59 |
So you're just doing this as an exercise right? Because otherwise Sequelize exists and mostly does what you want, I think?
|
|
# ? Mar 26, 2024 00:30 |
|
Why did I think I shouldn't use sequelize, I thought a long time ago everyone switched to knex. Then it was prisma and now drizzle. Ultimately you're correct we don't need it but when I built my own testing framework for example I learned a heck of a lot. When I built my own web framework that has been the most useful thing ever, it's the best node web framework out there in my opinion. At least when it comes to apis and simple sites, it can't do the fancy things that next can do but it's really easy to use and does everything I need, it's the only one out there that does cors properly. Ultimately, I learned a lot, and this sql orm is the same. Although, my sabbatical is ending and I don't think I'm going to finish it in time. It's turned out to be about 10 times harder than a web framework to make. Nolgthorn fucked around with this message at 11:08 on Mar 26, 2024 |
# ? Mar 26, 2024 11:03 |
I mean, in my opinion ORMs are cursed anyway, especially the ones that encourage active-record stuff. I think repository methods that do regular queries combined with proper result type checking with e.g. Zod are a cleaner way to interact with your DB, and have the added benefit of letting you already get out of db-land and into domain logic. That said, even if it's an exercise I think you could probably get more guidance on your path by peeking at the source code for Sequelize and Zod than from me trying to parse what you're doing and giving targeted advice. Either way, respect for trying something out and posting about it, it's interesting to read!
|
|
# ? Mar 26, 2024 14:44 |
Given I have a bunch of rectangular coordinates for blocks of text, what would be the best way to add them as selectable text blocks to a page and ensure their text content fits within the coordinates best it can?
|
|
# ? Mar 27, 2024 21:48 |
|
You have to do something like render it on the page with no height or width, measure it, then adjust the size using a percentage. There's no easy way to do it because different fonts and operating systems and browsers and etc.
|
# ? Mar 27, 2024 23:43 |
|
Nolgthorn posted:You have to do something like render it on the page with no height or width, measure it, then adjust the size using a percentage. There's no easy way to do it because different fonts and operating systems and browsers and etc.
|
# ? Mar 28, 2024 00:25 |
|
I've just started migrating my big javascript game project from being bare javascript drawing everything on a canvas (which worked okay!) to using svelte, which in general feels like a simplicity improvement, except when I run into problems where css seems super reluctant to do the layout things I want. One in particular that seems to be a recurring bugbear for many people (such that stackoverflow answers seem to mostly end with "here's a poo poo option that kinda vaguely worked for this one specific situation") and which I can't find a good answer to, is I frequently want to make an element have a fixed aspect ratio and [otherwise] take up all the space available to it. (e.g. a square element in a wide rectangle should be square with equal height, centered, and in a tall rectangle should be a square with equal width, centered.) css has an aspect-ratio style but it seems to be lower precedence than width and height, so width: 100%, height: 100%; aspect-ratio: 1, for example, just ignores the aspect ratio. You can get what I want in the *full screen* by doing width: 100vw; height: 100vh; max-width: 100vh; max-height: 100vw, but there doesn't seem to be an equivalent of vw and vh for "the size of the container you're in". max-width: 100%; max-height: 100%; aspect-ratio: 1 works for *some* situations (where the child elements want to be big and also square) but doesn't work if the child elements are also wanting to decide their size based on the available space. I guess one option would be to bind the width and height of the parent element and change the style between `width: 100%` if parentHeight>parentWidth and `height: 100%' if parentWidth>parentHeight (with a fixed aspect-ratio either way), but I want to do this layout style quite frequently and all that binding would be a loving mess. Is there a way that works? (Parent element is often a cell of a display:grid element which may be making this better or worse since there's style elements for centering like that, but a grid element with width/height 100% seems frustratingly willing to break outside of the constraints it was given. In a pinch I could bind parent element dimensions and just do a lot of absolute positioning instead, but that would end up more like my old canvas app and I'm *trying* to take advantage of things that one would expect to make life easier!)
|
# ? Apr 9, 2024 03:49 |
|
Dumb Angular Question - and I'm phone posting so the code will be minimal, sorry. Essentially what I'm trying to do is dynamically loop over a collection of data and print out an HTML table with different types of user input controls. That all works well and good, but now a new requirement came in to also optionally show some checkbox next to the input control. code:
I have that dynamically being added in easily enough, but the problem is my first div takes the full width of the TD and then the checkbox appears on a new line beneath it. We'd like to instead shrink the input so the textbox and the checkbox are on the same line. When I inspect the HTML while running my app, that first div inside the TD doesn't have a "style: width" attached at all. And I have no idea why, because "cell.canBeNA" evaluates just fine when deciding to show my checkbox Any idea what stupid thing I must be overlooking?
|
# ? Apr 9, 2024 15:01 |
|
Sab669 posted:Dumb Angular Question - and I'm phone posting so the code will be minimal, sorry. Can I have a type for row.td? What is canBeNA? a boolean? is it an optional boolean? right now cell.canBeNA ?? 80 will return either the value of cell.canBeNA or 80, which I don't think is what you want. Also if you're using Angular 17, you should use the Control Flow syntax, not that it's required to solve your issue. EDIT: I'm going to assume you should replace [style.width.%]="cell.canBeNA ?? 80" with something more like [ngStyle]="{'width' : cell.canBeNA ? '80%' : null}" The Merkinman fucked around with this message at 16:04 on Apr 9, 2024 |
# ? Apr 9, 2024 16:01 |
|
The Merkinman posted:Can I have a type for row.td? row.td is just some collection of a custom class which spells out all sorts of stuff like labels, input types, colspan and rowspan and input masking and sorts of attributes associated with an HTML table, generally. But yea, canBeNA is a bool and you're right that it was mostly just a syntax error. This seems to have done the trick: [style.width.%]="cell.canBeNA ? 80 : auto"
|
# ? Apr 9, 2024 16:43 |
|
|
# ? Jun 5, 2024 05:26 |
|
roomforthetuna posted:I frequently want to make an element have a fixed aspect ratio and [otherwise] take up all the space available to it. (e.g. a square element in a wide rectangle should be square with equal height, centered, and in a tall rectangle should be a square with equal width, centered.) If the element is an <img> or <canvas> etc you can use CSS object-fit to format it like a background image (contain, cover, etc.) to fit the container. Another approach is to size with a CSS unit that's the same horizontally and vertically (ie vw or vh but not both) so your code might look like <div style="width: 5vw; height: 5vw">...</div>. I'm guessing the hacky approaches you've found are those old padding-bottom-based approaches and yeah, I don't like those either.
|
# ? Apr 9, 2024 22:03 |