Giter Club home page Giter Club logo

fjs's Introduction

FJS

I'll give you one guess what the F stands for...

This is a sensible C# inspired take on JavaScript's crappier nearly unusable stock objects.


High-level

The number of times I have had a head slapping moment using JS has formed a little crater in my forehead. For a language that we are all forced to use for web development, it's quite lacking. It has gotten better over the years, but there are still some things to this day that are just embarrassing. So I thought it would be fun to replicate C# objects using JS to see how far I could take irony of developing in JS to use C# objects in JS ultimately. If you haven't figured it out already this is mostly a joke. So if you are getting upset don't; I am just poking fun at the Stockholm syndrome of programming languages.

Below is a list of what I have implemented so far. I am only planning on adding things as needed.

File organization

I am organizing the TypeScript classes and modules as logically as I can to follow C# namespacing. Therefore, everything will start in the System folder like it has done for many years in mscorlib.dll.

DateTime

JavaScript's Date object is one of the most ubiquitously hated objects to use because there are some very unwelcomed biases that the creator(s) introduced. Unlike C#'s pristine example of what a DateTime library should be. I have written a wrapper around the nonsensical Date object to make it mirror the dot net System.DateTime struct.

There are so many problems with JS Date it gets its own ReadMe.

TimeSpan

JavaScript's woefully lacking Date object does not support reasonable date math. Therefore, there is no exact JS equivalent TimeSpan object unlike the wonderous joy it is to use C#'s System.TimeSpan struct. The closest thing that JS has is the getTime() function which returns the total number of milliseconds since 01/01/1970 UTC which is a designated epoch.

IObject{T}

Everything in JS inherits from Object.prototype, just like everything in C# extends System.Object. The concepts are the same, but unfortunately there was no easy way to encapsulate this object without making life miserable. Therefore, I opted for making an interface instead to help provide guidance on getting closer to a C# object.

Here is a list of key differences between the two object constructs:

C# Method JS Function Notes
Equals N/A Beyond using the === operator, that's it. There really isn't a way to perform equality checks on two user made objects.
GetHashCode N/A This is totally a foreign concept in JS.
ToString toString This does exist and can be overridden (shadowed).

This interface accepts a generic of type T so that the equals function knows what it is trying to equate to. This diverges from what C# does, but this exercise is never going to be perfectly replicate C#. It's just going to better than what JS has to offer today. Therefore, there isn't a benefit to implementing an equals method that takes object.prototype or any type as an argument for compare.

IEquatable{T}

https://learn.microsoft.com/en-us/dotnet/api/system.iequatable-1

There is no equivalent in JS for this. I just wanted to follow the same pattern provided in C# where you would implement this interface to get additional benefits from collections and other objects that need the Equals(T) method.

There is overlap between this interface and IObject{T} and that's on purpose.

IComparable{T}

https://learn.microsoft.com/en-us/dotnet/api/system.icomparable-1

There is no equivalent in JS for this. It is required for sorting objects.

IEqualityComparer{T}

https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.iequalitycomparer-1

There is no equivalent in JS for this. I implemented it so I could use it with my Dictionary<TKey, TValue> implementation.

KeyValuePair{TKey, TValue}

https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.keyvaluepair-2

You could argue that there are equivalents for this in JS, but not really. This is used in conjunction with my Dictionary<TKey, TValue> implementation.

List{T}

This is an amalgamation of System.Array and List<T> to combat the inconsistencies of the thing called an Array in JS. You know this thing []. The Arrays in JS are confused because they don't know if they want to be arrays or stacks.

The one glaring problem that became terribly clear to me while working on this humanitarian effort is that JS Arrays are mutable. I don't know why that never dawned on me before. Something always really bugged me about JS Arrays, but I have finally nailed it.

JS Arrays, really shouldn't be called Arrays - it's a poorly managed array that wants to be a stack sometimes at best.

There are so many problems with JS Arrays it gets its own ReadMe.

Dictionary{TKey, TValue}

JS Maps are actually not that bad, but they still kind of suck for one reason. JS Maps, JS as a whole really, has zero clue how to index custom objects. If you are using anything other than a primitive for a key, your Map is absolutely useless. JS Maps will let you add duplicate keys, because again it has zero clue how to do comparisons. This is what I would refer to as an incomplete feature. I can't call it a bug because JS has no concept of hashing.

There are several problems with JS Maps, so it too gets its own ReadMe.

Hashing and Equality

Since hashing isn't a thing in JS I had to come up with some ways to do it. And when I say come up with ways to do it I mean rip off C# as best as I can. Shockingly I was able to implement equivalent hashing algorithms for both System.DateTime and System.string. I honestly didn't think it could be done, but alas here we are and we are better for it :D.

This is a complicated subject, so it gets its own ReadMe.


I still don't know what the F stands for

What does the F stand for in BFG 9000? If you can't answer that question, you are making me feel old and that's rude.

Final words

Lastly I just want to shout myself out for doing this for the world. It couldn't have been done without my selfless efforts.

We can do better than JavaScript. Please, think of the children.

~ YOU'RE WELCOME ~

Pascal I did it!

fjs's People

Contributors

dyslexicanaboko avatar

Stargazers

Blayze Turbo avatar

Watchers

 avatar

fjs's Issues

Test hashing algorithms

  • Need to setup some basic tests for hashing algorithms.
  • Want to make sure I didn't miss anything with what I ported over from C#.
  • Are there any other hashing algorithms needed?

Guid implementation part 2

I am not sure how far I want to go with this because it can get very involved.

Hashing algorithm:
https://github.com/microsoft/referencesource/blob/master/mscorlib/system/guid.cs#L923

The algorithm is straight forward as far as I am concerned, the thing I am not sure I want to get into is breaking the hex down into its individual parts of int, shorts and bytes.

I know what it takes to increment guids, so I am not sure if I want to go as far as making them sortable in JavaScript too. One could argue that treating them as strings is good enough. Same with the hash. The main point of this class is to encapsulate a Guid string in a concrete type so it's easier to identify.

Guid implementation

  • Is it possible to have a Guid implementation?
  • Not to generate GUIDs, but instead to contain them as a proper type?
  • Is it worth it to break them down into their individual number segments?

List<T> cannot handle non-sequential arrays

  • The List<T> cannot handle non-sequential JS Arrays. Meaning if an argument of [undefined, 1, 2, 3] is provided to the constructor, a failure will occur immediately as a zero index is expected.
  • There is no way to combat this, I think raising an error is the only sensible thing that can be done.

Add more DateTime Static functions

Static functions

  • DateTime from milliseconds. Convenience function to stop doing this: new DateTime(new Date(milliseconds))
  • There is already a parse() function, but I need to make sure that initializing UTC dates is working properly.

Instance function

  • Provide a toUtc() function to get back a JS Date with UTC data - regardless of whatever time zone it's going to display. This is for the purposes of saving data in the database.

Use case

  1. UTC from API
  2. Import to show as local time
  3. User modifies date
  4. Convert back to date with UTC
  5. Save via API

Equality and Comparisons

Handling equality and comparisons for List<T>

  • Modify List<T> constructor to give an optional way to provide a IComparer of T or equivalent for the following functions:
    • contains(item: T): boolean
      • This function will operate based on how equality and comparison is implemented in T
    • removes(item: T): boolean
      • Works like contains
    • sort(comparison?: (left: T, right: T) => number): void
      • This function will operate based on how equality and comparison is implemented in T or the user can provide their own sort function. As of right now a default comparer is inside of the class, but it will only work for primitives.

Handling equality and comparisons for non-primitive types

Unanswered questions

  • Can TypeScript handle complex objects of type T providing guidance on equality and comparison?
  • Can the List<T> automatically pick up how to compare complex T from T?
    • If yes, then great, that's how C# does it.
    • If no, then that kind of sucks, but the constructor can just take an optional comparer object

Publish as NPM package

  • Have no clue how to do this.
  • Need to research.
  • Publish this library as an NPM package so I can use it in my other project.

Managed arrays as List<T>

Original hypothesis

I find the stock functionality of the JS array disappointing to say the least. Therefore, I will be attempting to implement:

  • Array
  • List<T>
  • Dictionary<K, V>

Not sure how that will go, but we'll see.

09/04 Update

  • This was far more involved than I expected. It started off simple, then got more complicated as I dove further into array manipulation. I learned exactly what it was that had been rubbing me the wrong way all this time about JS arrays.
  • As a result I have ditched the idea of implementing my own type of Array class to mimic the System.Array class from C#. It immediately became an absurd idea after realizing that TypeScript already has a typed Array<T> construct. This was an excellent jumping off point for me, no reason to change it.
  • I have put full focus into working on only List<T> for this ticket.
  • I have run into an interesting problem of handling object equality and comparisons which will always be simple if you are only working with primitive types, but immediately default comparison does not work for composite types. Therefore, I created ticket #3 just for that.
  • Dictionary<K,V> obviously will need to be its own ticket all together. Can't even consider this until comparisons are handled. Stub ticket #4 created.

Update README

Properly fill out the README and provide extension readmes as needed.
Thinking one readme per major object, not sure yet.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.