Author: adiyatmubarak

Manipulasi Array di Javascript Dengan Paradigma Functional Programming

Akhir-akhir ini sering berurusan dengan javascript dan kebetulan belajar paradigma baru yang selama ini hampir tidak pernah dipake, yaitu functional programming atau biasa disingkat FP.

Salah satu konsep paling umum di FP adalah menghindari proses mutate atau melakukan perubahan state dari suatu data (immutable)

Contoh proses mutate adalah seperti berikut.

let names = ['alex', 'mike', 'carl', 'james']
console.log(names)
names.push('adam') // proses mutate
console.log(names)

Bisa dilihat contoh di atas bahwa terjadi perubahan state dari variable “names”, dimana semula berisi 4 item kemudian terjadi manipulasi state sehingga menjadi 5 item.

Sebagai dokumentasi pembelajaran saya, maka akan saya catat pada blog ini beberapa cara manipulasi array menggunakan paradigma FP dengan bantun fungsi spread pada javascript.

Add item baru pada array.

// Sebagai contoh kita ingin menambahkan `adam` ke dalam array.
let names = ['alex', 'mike', 'carl', 'james']
let newNames = [
  ...names,
  'adam'
]

Delete item pada array.

// Sebagai contoh kita ingin menghapus carl dari array.
// Ada 2 cara dengan cara filter dan menggunakan spread.
let names = ['alex', 'mike', 'carl', 'james']
let index = names.findIndex(item => item === 'carl')

let namesMethod1 = names.filter(item => item !== 'carl')
let namesMethod2 = [
...names.slice(0, index),
...names.slice(index + 1),
]

Edit item pada array.

// Sebagai contoh dengan data structure yang lebih komplex, kita ingin
// melakukan perubahan status pada order004 menjadi paid.
let orders = [
  {id: 'order001', status: 'unpaid'},
  {id: 'order002', status: 'unpaid'},
  {id: 'order003', status: 'unpaid'},
  {id: 'order004', status: 'unpaid'},
  {id: 'order005', status: 'unpaid'},
  {id: 'order006', status: 'unpaid'},
]
let index = orders.findIndex(order => order.id === 'order004')

let newOrders = [
  ...orders.slice(0, index),
  {
    ...orders[index],
    status: 'paid',
  },
  ...orders.slice(index + 1)
]

Contoh diatas adalah hal yang umum dalam manipulasi array di javascript, semoga bisa bermanfaat bagi pembaca.

Referensi:
https://jaketrent.com/post/remove-array-element-without-mutating/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

 

Compiling Proto File

I always forgot how to compile proto file and here is how compile .proto file for my personal note. 😀

$ protoc -I proto_dir/ proto_dir/proto_file.proto --go_out=plugins=grpc:proto

Example above for golang implementation, another programming language can use another out parameter.

  --cpp_out=OUT_DIR           Generate C++ header and source.
  --csharp_out=OUT_DIR        Generate C# source file.
  --java_out=OUT_DIR          Generate Java source file.
  --js_out=OUT_DIR            Generate JavaScript source.
  --objc_out=OUT_DIR          Generate Objective C header and source.
  --php_out=OUT_DIR           Generate PHP source file.
  --python_out=OUT_DIR        Generate Python source file.
  --ruby_out=OUT_DIR

Fix SQLite JOIN Query Without Result

I’m developing an API and using SQLite as my database and here is my table creation along with the relationship.

CREATE TABLE IF NOT EXISTS superhero (
    superhero_id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    gender TEXT
);

CREATE TABLE IF NOT EXISTS address (
    address_id INTEGER PRIMARY KEY AUTOINCREMENT,
    street TEXT,
    city TEXT,
    country TEXT,
    superhero_id INTEGER,
    FOREIGN KEY(superhero_id) REFERENCES superhero(superhero_id) ON DELETE CASCADE ON UPDATE CASCADE 
);

CREATE TABLE IF NOT EXISTS power (
    power_id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT
);

CREATE TABLE IF NOT EXISTS superheropower (
    power_id INTEGER,
    superhero_id INTEGER,

    FOREIGN KEY(power_id) REFERENCES power(power_id) ON DELETE CASCADE ON UPDATE CASCADE ,
    FOREIGN KEY(superhero_id) REFERENCES superhero(superhero_id) ON DELETE CASCADE ON UPDATE CASCADE 
);

But I got very weird issue that when I execute SQL query with JOIN, I always got no result. But it’s working with simple SELECT query.

After googling, I just know that SQLite is disabled the JOIN ability by default.
So we need to enable it when we create the table.
What you need to do, add this following syntax before your table creation.

PRAGMA foreign_keys = ON;

Reference: https://sqlite.org/foreignkeys.html#fk_enable

Fix OSX “dyld: Library not loaded”

I’m using OSX Mojave with zsh and all of sudden I always got an error when opening a new terminal session after doing an upgrade.

dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib
Referenced from: /usr/local/bin/awk
Reason: image not found

Seems like there’s an error from awk and I found the solution by upgrading gawk

$ brew upgrade gawk

Reference: https://stackoverflow.com/a/40281206/1936697

Fix Xiaomi INSTALL_FAILED_USER_RESTRICTED

If you are developing android and using real device to install your build APK instead of virtual device and you are using Xiaomi phone (Mine is Xiaomi Redmi 4x) probably you will facing this error.

To fix this, what you need to do are just enable these configuration in your device.

  • Developer Mode
  • USB Debugging
  • Install via USB
  • USB debugging (Security Settings)

Reference: https://stackoverflow.com/a/49485273/1936697

Fix Django-Jet Select Dropdown not Working on Custom Page

I’m having an issue when creating a custom page in django-jet. I’m using v1.0.6 and always got jQuery error in dropdown select in my custom page.

Uncaught TypeError: django.jQuery is not a function
at HTMLSelectElement.<anonymous> (bundle.min.js?v=1.0.8:1)
at HTMLSelectElement.dispatch (bundle.min.js?v=1.0.8:9)
at HTMLSelectElement.m.handle (bundle.min.js?v=1.0.8:9)
at Object.trigger (bundle.min.js?v=1.0.8:9)
at HTMLSelectElement.<anonymous> (bundle.min.js?v=1.0.8:9)
at Function.each (bundle.min.js?v=1.0.8:8)
at ot.fn.init.each (bundle.min.js?v=1.0.8:7)
at ot.fn.init.trigger (bundle.min.js?v=1.0.8:9)
at n.select (bundle.min.js?v=1.0.8:12)
at o.<anonymous> (bundle.min.js?v=1.0.8:12)

I solved my issue by adding jquery manually following below in my custom page.

{% block extrahead %}
{{ block.super }}


{% endblock %}

Hope this issue fixed in next release.

Reference: https://github.com/geex-arts/django-jet/issues/161#issuecomment-412071688

Running Specific Test in django-nose

Since I used django-nose for my project. Django-nose runner cannot behave like default django test runner.

Following command will not work in django-nose test runner.

# Run all the tests found within the 'animals' package
$ ./manage.py test appname

# Run just one test case
$ ./manage.py test appname.tests.TestCaseName

# Run just one test method
$ ./manage.py test appname.tests.TestCaseName.test_function_name

Because django-nose structured all the test in one package called tests following below.

tests:
   |- __init__.py
   |- test_views.py
   |- test_forms.py
   |- test_endpoints.py
   |- test_utils.py

Here is the alternative to run specific task in django-nose.

# Run all the tests found within the 'animals' package
$ ./manage.py test appname

# Run just one test case
$ ./manage.py test appname.tests.test_script:TestCaseName

# Run just one test method
$ ./manage.py test appname.tests.test_script:TestCaseName.test_function_name

Reference: https://stackoverflow.com/a/18834222/1936697