Feb 09, · 1) Create the basic structure. The following code will create a global variable accesible after the code is executed called myWindowGlobalLibraryName. . May 28, · In this tutorial I go over a super simple way to make your own library that is really similar to jQuery. I hope you like it! Please subscribe if you found th.
Libraries: we use them all the time. A library is packaged code that developers can use in their projects, which invariably saves work and prevents reinventing the wheel. Having reusable packages, either open creatf closed-source, is better than rebuilding the same feature, or manually copying and pasting from past projects. But other than packaged code, what is a library exactly?
With a few exceptions, a library should always be one file, or several in a single folder. Its code should be maintained separately and should remain as-is when implementing it in your project. Some devices, such as mice and keyboards, allow configuration through an interface provided with or by the device. In this article, I will explain how libraries are built.
Any code that makes achieving a certain goal easier and which can be reused, like an abstraction, is a candidate to be bundled into a library. An interesting example is jQuery.
Either way, it will benefit the library and all the projects depending on it. A popular open-source project can also lead to great opportunities. A company may be impressed by the quality of your mother teresa what was she famous for and offer you a job. Maybe a company will ask you to help integrate your project into crrate application.
After all, no one knows your library better than you. You can push your limits and try new things. Before writing the first line of code, it should be clear what the purpose of your library is—you have to set goals. With them, you can maintain focus on what problem you hope to solve with your library. Keep in mind that your library should be easier to use and to remember than the problem in its raw form. The simpler the API, the easier it will be for users to learn to use your library.
To quote the Unix philosophy:. Ask yourself: What problem does your library solve? How do you intend to solve it? No matter the size of the library, try to make a roadmap. List every feature you want, then scrap as many as you can until you have a tiny, but how to stop tooth pain fast at home library, much like a minimum viable product.
That will be your first release. From there, you can create milestones for every new feature. Believe me, this will keep you sane. Personally, I really like to approach my library from the perspective of the end user.
You could name it user-centric design. In essence, you are creating an outline of your library, hopefully giving it more thought and making it more convenient for whoever chooses to use it. At the same time you get to think about which aspects should be customizable, something discussed later in this article. The ultimate API quality test is to eat your own dog food, to use your library in your own projects.
Try to keep the library as bare as possible, while keeping it flexible enough to make it work for their edge-cases too, through customization as described later in this article.
Depending on the complexity of your library, you may also want to give some thought to structuring. Utilizing design patterns is a great way to structure your library, or even to overcome some technical problems. It also reduces the risk of refactoring large parts when adding new features. A perfect example of that is chart. Both are excellent libraries to visualize data. But if you need more control over graphics, D3. There are various ways to give control to the user: configuration, exposing public methods and through callbacks and events.
Configuring a library is often done during initialization, but some libraries allow you to modify options during run-time. Methods can be exposed to cerate with an instance, for example to retrieve data from the instance getters javacript, to put data in the instance settersand to perform actions.
Callbacks are sometimes passed with public methods, often to run javasctipt code after an asynchronous task. Events have a lot of potential.
Events are often used to indicate, you probably guessed, events! Much like a callback, you can provide additional information and return a value for the library to work with. In some cases, you may want to allow users to extend your library. For this, you can expose a public method or property users can populate, much like Angular modules angular.
To make sure extensions follow some convention, you can and should write documentation. Writing an outline makes a great start for test-driven development. In short, this is when you write down criteria in the form of tests, before writing the actual library.
If these tests check whether a feature behaves like it should and you write those before writing your library, libray strategy is called behavior-driven development. Either way, if your tests cover every feature in your library and your code passes all the tests, you can safely assume that your library works. These two testing frameworks are very popular, but there are many more in many flavors. My outline, created earlier in this article, already had comments on what the expected output is.
This is where all tests start: with an expectation. A Jasmine test for my library would look like this:. You may or may not use a module loader. However, the developer that chooses to implement your library might, so you will want to make your library compatible rceate module loaders.
But which one? That way you can use ES in your project, while still producing a library fit for all. Documentation should always start with basic information such as a project name and a description.
This starts with API documentation. Tutorials and examples make great additions, but writing these can be a lot of work.
These are comments that librar be parsed and converted to documentation pages with JSDoc. Some users may want to make changes to your library. In most cases this will be for contribution, but some may want to create a custom build for private use.
When you open-source your library, contributions are great. To guide contributors, you can add documentation in which you explain the steps for making a contribution and the criteria it should fulfill.
It will make it easier for you to review and accept contributions, and for them to do it right. How to clean glass coffee carafe but not least, include a license. Technically, if you choose not to include one, it will still be copyrighted, but not everyone knows that.
I what is the most economical diesel car in ireland ChooseALicense.
Versioning is essential for a good library. If you ever choose to make breaking changes, a user probably wants to keep using the version that works for them. The current de-facto standard for version naming is Semantic Versioningor SemVer.
Tto versions consists of three numbers, each indicating a different change: major, minor and patch. If you have a git repository, you can add version numbers to your repository. You could consider them snapshots of your repository.
Tagswe call them. How to remove a broken locking lug nut create a tag, open the terminal and type:. Many services, hw GitHub, will provide an overview of all your versions and download links for each. Many programming languages come with a package manager, or have third party package manager available. These allow us to pull in libraries specifically for those languages. By default, your npm package will be published publicly.
Fear not! You can also publish private packagesset up a private registry or completely avoid publishing at all. To publish your package, your project will need a package. You can do that manually or use the interactive wizard. To start the wizard, type:. The version property should match your git tag.
TABLE OF CONTENTS
Reusing code in programming is a key to become a productive engineer. Code can be reused in different ways-make a library and reuse it, is one of the options. This way we can use our code across the multiple projects. We can even make it open-source so other people around the globe could use your code, how awesome is that. In first glance, we think that creating a library is - advance user's work, but I must say that, it's not true, even beginners can create a library, it's really simple.
Typescript allows us to write type information for the value, which can be very powerful because it saves you time catching errors and providing fixes before you run your code. The main benefit of writing our library in Typescript is, our text editor gives us better information about our methods and properties.
Now we know what is Typescript, and why we need it, let's go ahead and take a look, what we need in order to build our library. Let's create a folder called uppercase-lib then navigate into that folder in the terminal and run following command.
Since, we will be using Typescript, we need to install Typescript. To install typescript run following command. Well, we will be using out Typescript only for development, once we build and ship the library, we won't need typescript there. So, by adding -D we are saying that we need to add this dependancy to dev dependancies.
If you don't know what are the dependancies and dev-dependancies, then here is a great article on that. We also need to install Typescript compiler or we can call it a CLI tool also called tsc. This will also create a file called tsconfig.
This file basically has all our typescript configuration. In tsconfig. There is a key called compilerOptions and it has key and value pairs inside it.
Some of them are commented out. In our case commonjs is perfectly fine. It generates corresponding '. It generates a source map for each corresponding '. I want my build should be in lib folder, therefore I will make changes to this line and after changing it will look like this - "outDir": "lib". Finally, we have to tell Typescript that where our source code will be located, in order to do that, we will add an array to our existing configuration.
Let's create a folder called src inside of project root. It is conventional that our src folder should have a file called index. Let's create that file. We will write our uppercase logic inside this file. This file is an entry point for our library. Here simply, we exported an arrow function which has logic to make a string upper case.
You may read about it here. Also you can see that, we have used types to tell the function that, what type of data we will be receiving and what type of data will be returned. Let's open our package. We need to modify some of the entries in this file. We need to add a build script, to do that add one more entry inside the scripts object:.
Now, we should see a new lib folder inside project root and that is our library build. First of all we need to create a. We only want to publish lib folder with other files. If we are using git and it has.
It will use. In order to publish a package from local computer you need to be logged in npm registry inside the terminal. But before that you need to have an account on npm registry. This will prompt you for username, type your username, press enter, then it will prompt you for the password, type your password then it will prompt you for email, type email and boom!
Now go and check your admin area on npm website and you will see your just-published package there. Now open our project in code editor and create a new file called app. As you can see here, we can see, now we all exactly know that, what our function will take-in and what it will return, thanks to the Typescript. That's it for this article, if you find this article useful then don't forget to share on social media and spread the world.